1

Let assume page url /xyz. I have window object declared at the top of my JS (because I need that throughout) like

window.myObject={ someWork:true } 

and I am changing the property value like

if(window.myObject.someWork){ //Do Work window.myObject.someWork=false } 

When I click back button from some other page (abc->xyz) my window.myObject.someWork remains false instead of getting re-initialised again at the top (because I think refresh will run the script again). Can you please suggest what is happening around and how to fix this.

Went through this Does back/forward in the browser change javascript variables?. Any fix for this? I am using Chrome and would like to make it work on all browsers

Fixed: Xufox solution is good and worked.It also cache DOM elements on back button. So make sure its not screwing up the code :)

Thanks in advance

7
  • It works fine for me, using Firefox 37. Please add more details or try to reproduce it in a JSFiddle. Commented May 11, 2015 at 6:54
  • 1
    it works fine for me too, jsfiddle.net/r8nfvn8u/5. Btw you can directly do myObject instead of window.myObject since if you dont declare it is by default taken as window object Commented May 11, 2015 at 6:56
  • I don't think that is the case. You probably have a little more of code that you didn't show to us or your second code executes immediately after first Commented May 11, 2015 at 6:56
  • Ok. Working fine on refresh but giving problem on back button. Update the question. Creating fiddle so please give me some itme Commented May 11, 2015 at 7:01
  • The back button usually fetches a cached state of the site… Commented May 11, 2015 at 7:01

1 Answer 1

2

When you hit the browser’s back button some browsers just get a cached state of the page. This saves some resources as the state of the document usually doesn’t need to change much when going back and forth in the browser history.

There’s a simple fix for that: add an event listener with a dummy function that fires when the page unloads (beforeunload or unload):

// either … window.addEventListener('beforeunload',function(){}); // … or window.addEventListener('unload',function(){}); 

Any appearance of an unload listener seems to clear all the stored variables and states, etc.


Complete code

window.myObject={ someWork:true }; if(window.myObject.someWork){ //Do Work window.myObject.someWork=false; } window.addEventListener('unload',function(){}); 

Also read this

Sign up to request clarification or add additional context in comments.

11 Comments

Seems a good play around but unfortunately this is still not working. I am using chrome
I’ve found a much simpler solution, wait a minute.
Okay, so I’m in Firefox 37 and it works perfectly fine… maybe someone else can help with that?
Cool. +1 for the answer but still waiting for an accepted one and browser free solution. I am also searching extensively about this. Will let you know
Yeah Got Working. Along with variable it was caching my hidden elements also so was getting different value which was not letting me processed further. Thanks for your help mate :)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.