I'm new to Javascript and web programming in general, so this may be a silly mistake. Nevertheless, I've had problems finding information about it.
I am developing a game in Javascript, where the player can walk around and move from scene to scene by clicking and having their avatar walk to different buildings/objects. For now, I just have the page change location, by calling this.gotoTimer = setTimeout(self.location = this.targetURL, 1000);
if a collision is detected.
The page links fine in both browsers. The problem is when the user presses the back button...In Chrome, everything resets; The player spawns in the same area as I have it set up in code, and all of the 'linktargets' are still valid. This is the desired behavior.
In Firefox, once the back button is pressed and the page reloads, the player is at his/her last known position, and the linktarget they visited will no longer link...I can eventually deactivate all of them by visiting them and pressing the back button.
From what I've been able to research, it seems to be a problem with how Firefox treats the cache, and the workaround I've been able to find involves appending a random number or the time to the Javascript files. This strikes me as gross. I may also be way off.
So I'm wondering two things:
- Is this an accurate assumption of the problem, or is there another issue?
- If it is, what is the best way to append these numbers. The entire concept seems very hacky to me...
Sorry to abandon the question for so long, but I found a solution that worked!
I now call window.location.reload(true); before I call window.location = whateverUrl;
...I'm not sure why it works so well (I figured window.location.reload would reload the current page before going to the new one [that obviously isn't what I would want]), but it seems fine. It may be doing something I don't know behind my back I guess, but functionally it is doing what I want.
I think that https://developer.mozilla.org/En/Using_Firefox_1.5_caching will answer all your questions. Firefox doesn't actually reload the page when going back, it rather restores it along with its JavaScript state. So you have several options:
unload
event handler to disable this behavior (suboptimal, degrades performance).window.location.replace()
instead of assigning towindow.location
, this will prevent the user from going back (undesirable I guess).pageshow
event handler to reset JavaScript state.