回到 Firefox 历史后,JavaScript 将无法运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2638292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
After travelling back in Firefox history, JavaScript won't run
提问by Patonza
When I use the back button on Firefox to reach a previously visited page, scripts on that page won't run again.
当我使用 Firefox 上的后退按钮到达以前访问过的页面时,该页面上的脚本将不会再次运行。
Is there any fix/workaroundto have the scripts execute again when viewing the page the second time?
第二次查看页面时,是否有任何修复/解决方法让脚本再次执行?
Please note that I have tested the same pages on Google Chrome and Internet Explorer and they work as intended.
请注意,我已经在 Google Chrome 和 Internet Explorer 上测试了相同的页面,并且它们按预期工作。
Here are the files and the steps I used to test the problem:
以下是我用来测试问题的文件和步骤:
(navigate to 0.html, click to get to 1.html, back button)
(导航到 0.html,点击进入 1.html,返回按钮)
0.html
0.html
<html><body>
<script>
window.onload = function() { alert('window.onload alert'); };
alert('inline alert');
</script>
<a href="1.html">Click Me!</a>
</body></html>
1.html
1.html
<html><body>
<p>Go BACK!</p>
</body></html>
回答by
Set an empty function to be called on window.onunload:
设置要在 window.onunload 上调用的空函数:
window.onunload = function(){};
e.g.
例如
<html><body>
<script type="text/javascript">
window.onload = function() { alert('window.onload alert'); };
window.onunload = function(){};
alert('inline alert');
</script>
<a href="1.html">Click Me!</a>
</body></html>
Source:http://www.firefoxanswer.com/firefox/672-firefoxanswer.html(Archived Version)
来源:http : //www.firefoxanswer.com/firefox/672-firefoxanswer.html (存档版)
回答by bobince
When I use the back button on Firefox to reach a previously visited page, scripts on that page won't run again.
当我使用 Firefox 上的后退按钮到达以前访问过的页面时,该页面上的脚本将不会再次运行。
That's correct and that's a good thing.
这是正确的,这是一件好事。
When you hit a link in Firefox (and Safari, and Opera), it does not immediately destroy your page to go onto the next one. It keeps the page intact, merely hiding it from view. Should you hit the back button, it will then bring the old page back into view, without having to load the document again; this is much faster, resulting in smoother back/forward page transitions for the user.
当您在 Firefox(以及 Safari 和 Opera)中点击链接时,它不会立即破坏您的页面以进入下一个页面。它使页面保持完整,只是将其隐藏起来。如果您点击后退按钮,它会重新显示旧页面,而无需再次加载文档;这要快得多,从而为用户带来更平滑的后退/前进页面过渡。
This feature is called the bfcache.
此功能称为bfcache。
Any content you added to the page during the user's previous load and use of it will still be there. Any event handlers you attached to page elements will still be attached. Any timeouts/intervals you set will still be active. So there's rarely any reason you need to know that you have been hidden and re-shown. It would be wrong to call onloador inline script code again, because any binding and content generation you did in that function would be executing a second time over the same content, with potentially disastrous results. (eg. document.writein inline script would totally destroy the page.)
您在用户之前加载和使用页面期间添加到页面的任何内容仍将存在。您附加到页面元素的任何事件处理程序仍将被附加。您设置的任何超时/间隔仍将处于活动状态。因此,您几乎没有任何理由需要知道您已被隐藏并重新显示。onload再次调用或内联脚本代码是错误的,因为您在该函数中所做的任何绑定和内容生成都会对相同的内容再次执行,这可能会导致灾难性的结果。(例如document.write,内联脚本会完全破坏页面。)
The reason writing to window.onunloadhas an effect is that the browsers that implement bfcache have decided that?—?for compatibility with pages that really do need to know when they're being discarded?—?any page that declares an interest in knowing when onunloadoccurs will cause the bfcache to be disabled. That page will be loaded fresh when you go back to it, instead of fetched from the bfcache.
写入 towindow.onunload有效果的原因是实现 bfcache 的浏览器已经决定?-?为了与真正需要知道何时被丢弃的页面兼容?-?任何声明有兴趣知道何时onunload发生的页面将导致 bfcache 被禁用。该页面将在您返回时重新加载,而不是从 bfcache 中获取。
So if you set window.onunload= function() {};, what you're actually doing is deliberately breaking the bfcache. This will result in your pages being slow to navigate, and should not be used except as a last resort.
所以如果你设置了window.onunload= function() {};,你实际上在做的是故意破坏 bfcache。这将导致您的页面导航缓慢,除非作为最后的手段,否则不应使用。
If you do need to know when the user leaves or comes back to your page, without messing up the bfcache, you can trap the onpageshowand onpagehideevents instead:
如果您确实需要知道用户何时离开或返回您的页面,而又不弄乱 bfcache,您可以改为捕获onpageshow和onpagehide事件:
window.onload=window.onpageshow= function() {
alert('Hello!');
};
回答by Mika Tuupola
You can check the persistedproperty of the pageshowevent. It is set to false on initial page load. When page is loaded from cache it is set to true.
您可以检查事件的persisted属性pageshow。它在初始页面加载时设置为 false。当页面从缓存加载时,它被设置为 true。
window.onpageshow = function(event) {
if (event.persisted) {
alert("From bfcache");
}
};
For some reason jQuery does not have this property in the event. You can find it from original event though.
出于某种原因,jQuery 在事件中没有这个属性。你可以从原始事件中找到它。
$(window).bind("pageshow", function(event) {
if (event.originalEvent.persisted) {
alert("From bfcache");
}
});
回答by Michael A.
A simple way to cause a page to execute JavaScript when the user navigates back to it using browser history is the OnPopState event. We use this to pause and replay the video on our home page (https://fynydd.com).
当用户使用浏览器历史记录导航回页面时,使页面执行 JavaScript 的一种简单方法是 OnPopState 事件。我们使用它来暂停和重播我们主页 ( https://fynydd.com)上的视频。
window.onpopstate = function() {
// Do stuff here...
};
回答by Chris Haas
Wire in an "onunload" event that does nothing:
连接一个什么都不做的“onunload”事件:
<html><body>
<script type="text/javascript">
window.onload = function() { alert('window.onload alert'); };
window.onunload = function(){};
alert('inline alert');
</script>
<a href="1.html">Click Me!</a>
</body></html>
回答by systempuntoout
回答by Tamer
for some cases like ajax operations url change listener can be used
对于某些情况,例如 ajax 操作,可以使用 url 更改侦听器
$(window).on('hashchange', function() {
....
});

