How to wrap reset variables when navigating between pages (Chrome specific)?

I am working with html5 history api. When I click the link to the new page, then click the Back button. I see variables that persist their state between page loads.

Example:

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

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

      

This singlePageSessionId variable must be set once per actual page load. However, when I click the back button and then check the namespace value. SinglePageSessionId keeps a timestamp when I first went to the site. Since I am re-executing javascript, I expect the variable to be reset.

I either have a fundamental misunderstanding of how the javascript engine works between pages, or there is some strange behavior here.

Is there a way to make sure the variables get reset?

+3


source to share


1 answer


As far as I tried, I could not reproduce the problem you are talking about. Locally, I didn't have any problems when I went back and got a 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();

      



Perhaps you were unable to provide all the information about the situation?

0


source







All Articles