Differences between Chrome and Firefox: reloading a page using Javascript

I am new to Javascript and web programming in general, so this can be a dumb mistake. However, I had problems finding information about this.

I am developing a game in Javascript where a player can walk and move from scene to scene by clicking and using his avatar in different buildings / objects. At the moment, I just have a place to change the page, calling this.gotoTimer = setTimeout(self.location = this.targetURL, 1000); if a collision is detected.

Links to pages are perfectly displayed in both browsers. The problem is that the user clicks the back button ... In Chrome, everything is reset; The player appears in the same area as in the code, and all the "linktargets" remain valid. This is the desired behavior.

In Firefox, when the back button is pressed and the page reloads, the player is in their last known position, and the link they visited will no longer link ... I can end up disabling them all by visiting them and click the button "Back".

From what I was able to research, there seems to be a problem with how Firefox relates to the cache, and the workaround I could find involves adding a random number or time to Javascript files. It seems rude to me. I can go out too.

Therefore, I am interested in two things:

  • Is this the exact premise of the problem or is there another problem?
  • If so, then what is the best way to add these numbers. The whole concept seems to me very hoarse ...
+4
source share
2 answers

Sorry to leave this question for so long, but I found a solution that worked!

Now I'm calling window.location.reload (true); before i call window.location = whateverUrl;

... I'm not sure why it works so well (I realized that window.location.reload will reload the current page before moving on to the new page (which is clearly not what I want)), but it seems to be fine. Perhaps I am doing what I do not know behind my back, I think, but functionally it does what I want.

+2
source

I think https://developer.mozilla.org/En/Using_Firefox_1.5_caching will answer all your questions. Firefox does not actually reload the page when it returns, it rather restores it along with its JavaScript state. Thus, you have several options:

  • Add an unload event handler to disable this behavior (suboptimal, degrades performance).
  • Use window.location.replace() instead of assigning window.location , this will not allow the user to return (undesirable, I think).
  • Add the pageshow event pageshow to the reset JavaScript state.
  • Change the logic so that it no longer assumes that the user will leave and will not return after clicking the link.
+1
source

Source: https://habr.com/ru/post/1368992/


All Articles