简体   繁体   English

浏览器后退按钮不会触发javascript

[英]Browser back button doesn't trigger javascript

I'm trying to create a single-page application. 我正在尝试创建一个单页应用程序。

To allow the user to share links from some specific part of the site, I'm simulating navigation by inserting entries in the browser history. 为了允许用户共享网站某些特定部分的链接,我通过在浏览器历史记录中插入条目来模拟导航。

The problem is, when the user click in the browser's back button, javascript doesn't trigger again. 问题是,当用户单击浏览器的后退按钮时,javascript不会再次触发。 Is possible force it? 有可能用力吗?


I Found the answer in developer.mozilla.org . 我在developer.mozilla.org中找到了答案。 Just add a listener to 'popstate' event: 只需在'popstate'事件中添加一个侦听器:

window.addEventListener('popstate', function(event) {
 if (event.state) {
   myFunction();
 }
}, false);

"myFunction" will be executed every time the active history entry changes. 每当活动历史记录条目更改时,将执行“ myFunction”。

NO ! 不!

Thats the answer !!!! 多数民众赞成在答案! There are many security restrictions in browsers - necessarily, otherwise the internet would be more mayhem than it is. 浏览器中有许多安全限制-一定是这样,否则,互联网将比现在更加混乱。 While there are some browser 'back' functionalities available in javascript they are very limited. 尽管javascript中提供了一些浏览器“后退”功能,但它们非常有限。 Browsers should load a page as it was when first opened, and will not pull data from recent to the past. 浏览器应加载页面,使其与第一次打开时的页面一样,并且不会将数据从最近的数据拉到过去的数据。 A prime example is the form resubmission warning when going back - a simple check that the step back to previous page cannot be controlled by the page code, instead its the browser stepping in with a check. 一个很好的例子是回退时的表单重新提交警告-简单检查一下是否可以由页面代码控制退回到上一页,而是由浏览器进行检查。 To expect to do anything more elaborate would be a waste of your time. 期望做更多复杂的事情会浪费您的时间。

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

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