javascript 会话丢失 window.location.href

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11526643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 13:31:54  来源:igfitidea点击:

Session lost window.location.href

javascripthtmlsession

提问by user1410668

I'm trying to redirect the user to another page width:

我正在尝试将用户重定向到另一个页面宽度:

window.location.href = "url here" //relative link

but when I do, it clears my sessvars.keyPageArrayvariable. Anyone know how to keep a session variable after a redirect?

但是当我这样做时,它会清除我的sessvars.keyPageArray变量。有人知道如何在重定向后保留会话变量吗?

This is the sessvars library that I am using: http://www.thomasfrank.se/sessionvars.html

这是我正在使用的 sessvars 库:http: //www.thomasfrank.se/sessionvars.html

UPDATE: I used google chrome debugger and my script actually does work. For some crazy reason it only works if I'm monitoring it variable by variable but not when I simply run it normally without the debugger on? Why is that happening?

更新:我使用了 google chrome 调试器,我的脚本确实有效。出于某种疯狂的原因,它仅在我逐个变量地监视它时才有效,而当我只是在没有调试器的情况下正常运行它时就无效了?为什么会这样?

回答by Esailija

Just because you named a variable sessvarsdoesn't make it so. Use localStorage, sessionStorageor cookies depending on your need.

仅仅因为您命名了一个变量sessvars并不能使其如此。根据您的需要使用localStoragesessionStorage或 cookie。

All javascript context is unloaded and lost when the browser navigates to another page.

当浏览器导航到另一个页面时,所有 javascript 上下文都会被卸载并丢失。

Example using localStorage:

使用示例localStorage

window.onbeforeunload = function() {
    localStorage.setItem( "session", JSON.stringify( window.sessvars) );
};

window.onload = function() {
    window.sessvars = JSON.parse( localStorage.getItem( "session") || "{}" );;
};

回答by Naftali aka Neal

Sessions are on the server side.

会话在服务器端

Nothing to do with the browser.

与浏览器无关。

回答by CoursesWeb

JavaScript doesn't keep data after the page is closed, to open anoher page, or eaven on refresh. Eventualy you can store data in cookie.

JavaScript 不会在页面关闭、打开另一个页面或刷新后保留数据。最终您可以将数据存储在 cookie 中。

回答by Alexander Gharibashvili

One important thing causing the problem is when in server /phptmp directory does not exist or does not have writing permissions.

导致问题的一件重要事情是服务器中的 /phptmp 目录不存在或没有写入权限。