简体   繁体   English

使用AJAX返回按钮和刷新

[英]Back Button and Refresh with AJAX

I need a solution for the page refresh and the back button when using AJAX. 使用AJAX时,我需要一个页面刷新和后退按钮的解决方案。

I'm using simple javascript for the AJAX implementation. 我正在使用简单的JavaScript来实现AJAX。

If anybody could send a code snippet I would be very grateful. 如果有人可以发送代码片段,我将非常感激。

如果你正在使用jQuery,那就是历史插件

Here's a solution that I've used in some of my pages. 这是我在某些页面中使用的解决方案。 Add this to pages that changes are made at. 将此添加到进行更改的页面。

window.onbeforeunload = function() {
     window.name = "reloader"; 
}

this triggers when you leave those pages. 离开这些页面时会触发此操作。 You can also trigger it if there were changes made. 如果进行了更改,您也可以触发它。 So that it won't unnecessarily reload the page that needs reloading. 这样就不会不必要地重新加载需要重新加载的页面。 Then on pages that you want to get reloaded on after a the browser "back" use. 然后在浏览器“返回”使用后要重新加载的页面上。

if (window.name == "reloader") {
    window.name = "no";
    location.reload();
}

this will trigger a reload on the page you need reloading to. 这将触发您需要重新加载的页面上的重新加载。

我正在研究这个问题 ,请参阅我的问题 ,“更新”文本中的链接。

essentially, you need to use & monitor the hash portion of the url... 基本上,你需要使用和监控网址的哈希部分...

http://.../path?parms#hashpart

Whan you change the hash, iirc window.location.hash , it won't reload the page, but your ajax can monitor, and respond to it. 你改变了哈希,iirc window.location.hash,它不会重新加载页面,但是你的ajax可以监视并响应它。

尝试PathJS它不需要jQuery或任何其他额外的lib。

The onbeforeunload event can be useful to guard against refreshing but it fires if you navigate away or refresh. onbeforeunload事件可用于防止刷新,但如果您离开或刷新则会触发。 If you require that users login to the app you can always show a generic message advising against navigating away and refreshing. 如果您要求用户登录该应用程序,您始终可以显示一条通用消息,建议不要导航和刷新。 If users click your app log out button set a var to disable the warning. 如果用户单击您的应用程序注销按钮,请设置var以禁用警告。 Could probably also make a 'Close' button that does the same thing. 也许还可以制作一个“关闭”按钮来做同样的事情。

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

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