简体   繁体   中英

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

I am working with the html5 history 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. 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. Since I am re-executing the javascript I expect the variable to be reset.

I either have a fundamental mis-understanding of how the javascript engine works between pages or there is some weird behavior here.

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/

(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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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