简体   繁体   English

Chrome和Firefox之间的差异:重新加载运行Javascript游戏的页面

[英]Differences between Chrome and Firefox: Reloading a page running a Javascript game

I'm new to Javascript and web programming in general, so this may be a silly mistake. 我是Javascript和Web编程的新手,所以这可能是一个愚蠢的错误。 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. 我正在使用Javascript开发游戏,玩家可以通过点击并让他们的化身走到不同的建筑物/物体来四处走动并从一个场景移动到另一个场景。 For now, I just have the page change location, by calling this.gotoTimer = setTimeout(self.location = this.targetURL, 1000); 现在,我只是通过调用this.gotoTimer = setTimeout(self.location = this.targetURL, 1000);改变页面位置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; 问题是当用户按下后退按钮时...在Chrome中,一切都会重置; 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. 在Firefox中,一旦按下后退按钮并且页面重新加载,播放器就处于他/她最后的已知位置,他们访问的链接目标将不再链接...我最终可以通过访问它们并按下它们来停用所有这些位置返回键。

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. 从我能够研究的内容来看,似乎是Firefox处理缓存的问题,而我能够找到的解决方法是将随机数或时间附加到Javascript文件中。 This strikes me as gross. 这让我印象深刻。 I may also be way off. 我也可能会离开。

So I'm wondering two things: 所以我想知道两件事:

  1. Is this an accurate assumption of the problem, or is there another issue? 这是对问题的准确假设,还是存在其他问题?
  2. 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); 我现在调用window.location.reload(true); before I call window.location = whateverUrl; 在我调用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. ...我不确定为什么它运行得那么好(我认为window.location.reload会重新加载当前页面,然后再转到新的[显然不是我想要的]),但看起来很好。 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. 我认为https://developer.mozilla.org/En/Using_Firefox_1.5_caching将回答您的所有问题。 Firefox doesn't actually reload the page when going back, it rather restores it along with its JavaScript state. Firefox在返回时实际上并不重新加载页面,而是将其与JavaScript状态一起恢复。 So you have several options: 所以你有几个选择:

  • Add an unload event handler to disable this behavior (suboptimal, degrades performance). 添加unload事件处理程序以禁用此行为(次优,降低性能)。
  • Use window.location.replace() instead of assigning to window.location , this will prevent the user from going back (undesirable I guess). 使用window.location.replace()而不是分配给window.location ,这将阻止用户返回(我猜不想)。
  • Add a pageshow event handler to reset JavaScript state. 添加pageshow事件处理程序以重置JavaScript状态。
  • Change the logic so that it no longer assumes that the user will go away and never return after clicking a link. 更改逻辑,以便它不再假定用户将离开并且在单击链接后永远不会返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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