简体   繁体   English

如何使用ajax捕获后退按钮并刷新页面

[英]How to trap the back button and refresh the page using ajax

I have a web page which updates progressivly therefore I need the windows "back" button to call a function to refresh the data and not go back a page.我有一个逐步更新的网页,因此我需要 Windows 的“后退”按钮来调用函数来刷新数据而不是返回页面。 I have the first part working I trap the "back" button with我有第一部分工作我用“后退”按钮捕获

window.onbeforeunload = confirmExit();

and confirmExit()并确认退出()

function confirmExit() {
    switch (page) {
        case 2:
            countries();
            break;
         case 3:
            counties(page3id)
            break;
        case 4:
            cities(page4id)
            break;
    }
}

Which all works well but after the page is refreshed the back button default action kicks in and it loads the previous page.一切正常,但在页面刷新后,后退按钮默认操作启动并加载上一页。 I have tried returning true and false.我试过返回真假。 I would appreciate any help, thank you.我将不胜感激任何帮助,谢谢。

you need to add virtual sites to the browsers history.您需要将虚拟站点添加到浏览器历史记录中。 this way the back button will not lead to another website.这样后退按钮就不会导致另一个网站。

use history.pushState() to add virtual sites.使用history.pushState()添加虚拟站点。

Suppose http://mozilla.org/foo.html executes the following JavaScript:假设http://mozilla.org/foo.html执行以下 JavaScript:

 var stateObj = { foo: "bar" }; history.pushState(stateObj, "page 2", "bar.html");

This will cause the URL bar to display http://mozilla.org/bar.html , but won't cause the browser to load bar.html or even check that bar.html exists.这将导致 URL 栏显示http://mozilla.org/bar.html ,但不会导致浏览器加载 bar.html 甚至检查 bar.html 是否存在。

Suppose now that the user now navigates to http://google.com , then clicks back.现在假设用户现在导航到http://google.com ,然后点击返回。 At this point, the URL bar will display http://mozilla.org/bar.html , and the page will get a popstate event whose state object contains a copy of stateObj.此时,URL 栏将显示http://mozilla.org/bar.html ,页面将获得一个 popstate 事件,其 state 对象包含 stateObj 的副本。 The page itself will look like foo.html, although the page might modify its contents during the popstate event.页面本身看起来像 foo.html,尽管页面可能会在 popstate 事件期间修改其内容。

If we click back again, the URL will change to http://mozilla.org/foo.html , and the document will get another popstate event, this time with a null state object.如果我们再次单击返回,URL 将更改为http://mozilla.org/foo.html ,并且文档将获得另一个 popstate 事件,这次是一个空状态对象。 Here too, going back doesn't change the document's contents from what they were in the previous step, although the document might update its contents manually upon receiving the popstate event.在这里,虽然文档可能会在收到 popstate 事件时手动更新其内容,但返回不会更改上一步中文档的内容。

from MDN: Manipulating the browser history来自MDN:操纵浏览器历史记录

the user can now press the back button the number of times you added virtual sites.用户现在可以按您添加虚拟站点的次数按后退按钮。 to detect the back button press use window.onpopstate检测后退按钮按下使用window.onpopstate

The popstate event popstate 事件

A popstate event is dispatched to the window every time the active history entry changes.每次活动历史条目更改时,都会向窗口分派 popstate 事件。 If the history entry being activated was created by a call to pushState or affected by a call to replaceState, the popstate event's state property contains a copy of the history entry's state object.如果正在激活的历史条目是通过调用 pushState 创建的或受调用 replaceState 影响的,则 popstate 事件的 state 属性包含历史条目状态对象的副本。

from MDN: Manipulating the browser history来自MDN:操纵浏览器历史记录

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

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