简体   繁体   English

如何在页面之间移动时重置变量状态(特定于Chrome)?

[英]How to reset variables state when moving between pages (Chrome Specific)?

I am working with the html5 history api. 我正在使用html5历史API。 When I click a link to a new page, and then click the back button. 当我单击指向新页面的链接,然后单击后退按钮。 I am seeing variables who are retaining their state between page loads. 我看到在页面加载之间保持状态的变量。

Example: 例:

(function(namespace){
    var singlePageSessionId = 0;
    singlePageSessionId = new Date().getTime();

    namespace.SinglePageSessionId = function(){
        return singlePageSessionId; 
    }
}(window.namespace = window.namespace || {}))

That singlePageSessionId variable should be set once per actual page load. 每个实际页面加载应该设置一次singlePageSessionId变量。 However when I click back, and then check the value of namespace.SinglePageSessionId it retains the timestamp of when I first navigated to the site. 但是当我单击后退,然后检查namespace.SinglePageSessionId的值时,它会保留我第一次导航到站点时的时间戳。 Since I am re-executing the javascript I expect the variable to be reset. 由于我重新执行javascript,我希望重置变量。

I either have a fundamental mis-understanding of how the javascript engine works between pages or there is some weird behavior here. 我或者对javascript引擎如何在页面之间工作有一个基本的误解,或者这里有一些奇怪的行为。

Is there a way to make sure the variables get reset ? 有没有办法确保变量重置?

How hard I tried I could not reproduce the issue you are talking about. 我试了多难我无法重现你所说的问题。 Locally I had no problem at all going back and receive new value. 在当地我完全没有问题回去并获得新的价值。 This is what I did http://jsfiddle.net/itaymer/bqg3azx8/ 这就是我做的http://jsfiddle.net/itaymer/bqg3azx8/

(function(namespace) {
    var singlePageSessionId = new Date().getTime();

    namespace.SinglePageSessionId = function(){
        return singlePageSessionId; 
    }
}(window.namespace = window.namespace || {}));

document.querySelector(".pageSessionId").innerHTML = window.namespace.SinglePageSessionId();

Maybe you failed to supply the whole situation information? 也许你未能提供全部情况信息?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM