Javascript 历史.back(); 不触发 $(document).ready();

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3628536/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:28:54  来源:igfitidea点击:

history.back(); doesn't trigger $(document).ready();

javascriptjqueryjavascript-eventsbrowser-history

提问by Sirber

I have a webpage that use $(document).ready()to build the interface. Then the user can go to a child page, and to go back to the original page he can press the browser's "previous" button or a "Return" button in the page which triggers a history.back();. Back on the original page, $(document).ready()is not triggered so the page is missing informations.

我有一个$(document).ready()用于构建界面的网页。然后用户可以转到子页面,并返回原始页面,他可以按浏览器的“上一个”按钮或页面中的“返回”按钮触发history.back();. 回到原来的页面,$(document).ready()没有触发所以页面缺少信息。

is there a way to trigger it automatically like if it was a "real load"?

有没有办法像“真正的负载”一样自动触发它?

thank you!

谢谢你!

edit

编辑

placing an alert in it, the alert is poped but stuff is missing in my interface like if some part of the ready event is missing. Investigating...

在其中放置警报,警报会弹出,但我的界面中缺少某些内容,就像缺少准备好的事件的某些部分一样。正在调查...

edit 2

编辑 2

hahahahaha in document.ready I clicksome checkbox which are supposed to be unchecked. When I "back" on this page, they are checked so they become unchecked because I reclickthem.

hahahahaha 在 document.ready 中我click有一些应该取消选中的复选框。当我“返回”此页面时,它们已被选中,因此它们变为未选中状态,因为我是click它们。

Sorry, this is completely my bad :(

对不起,这完全是我的错:(

回答by stonyau

A quick solution to this problem, use "onpageshow" instead.

此问题的快速解决方案,请改用“onpageshow”。

window.onpageshow = function(event) {
    //do something
};

回答by Paulo Santos

If the user uses the Backbutton to navigate and you require a full reload of the page, you can set the NO-CACHEpolicy of the page.

如果用户使用Back按钮导航并且您需要完全重新加载页面,则可以设置NO-CACHE页面的策略。

This way the browser is forced to reload the page from the server, even using the Backbutton.

这样,即使使用Back按钮,浏览器也被迫从服务器重新加载页面。

回答by BGerrissen

1.) put scripts at the bottom of your page.
2.) execute plugins and whatnot in your last script tag(s).
3.) Do not use onDomReady implementations at all, it's redundant.

1.) 将脚本放在页面底部。
2.) 在最后一个脚本标签中执行插件等。
3.) 根本不要使用 onDomReady 实现,它是多余的。

People are so accustomed to onload or ondomready, they overlook the fact that putting your scripts at the bottom of a page does virtually the same thing without the need to poll and see if your html is available.

人们习惯于 onload 或 ondomready,他们忽略了这样一个事实:将脚本放在页面底部几乎可以做同样的事情,而无需轮询并查看您的 html 是否可用。

Furthermore, it's also good practise as your scripts do not block html/css rendering either.

此外,这也是一种很好的做法,因为您的脚本也不会阻止 html/css 呈现。

Not depending on onDomReady or onLoad implementations solves a lot of issues.

不依赖于 onDomReady 或 onLoad 实现解决了很多问题。

回答by Dutchie432

Very interesting question. You might need to re-trigger the event/function when the page gets focus, or something similar. you might also need to keep a flag variable to track whether an 'event re-triggering' is in order.

很有趣的问题。当页面获得焦点或类似的东西时,您可能需要重新触发事件/函数。您可能还需要保留一个标志变量来跟踪“事件重新触发”是否正常。