gpt4 book ai didi

Javascript 跨页面存储信息

转载 作者:行者123 更新时间:2023-12-02 15:27:55 25 4
gpt4 key购买 nike

我一直在尝试使用 sessionStorage 跨页面保存 JavaScript 变量。

据我了解,语法是:

sessionStorage.setItem('whateverkey', 'whatever value');

有没有办法在sessionStorage中保存变量,或者其他简单的方法来保存跨页面的信息

提前致谢!

最佳答案

My value is 'null' when I try to access it from another page.

这是exactly how sessionStorage is designed to work :

The sessionStorage property allows you to access a session Storage object. sessionStorage is similar to Window.localStorage, the only difference is while data stored in localStorage has no expiration set, data stored in sessionStorage gets cleared when the page session ends. A page session lasts for as long as the browser is open and survives over page reloads and restores. Opening a page in a new tab or window will cause a new session to be initiated, which differs from how session cookies work.

如果您希望数据在同一源的跨页面上保留,请改用 localStorage

关于Javascript 跨页面存储信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33553527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com