通过浏览器返回按钮到达时重新加载站点 [英] Reload the site when reached via browsers back button

查看:466
本文介绍了通过浏览器返回按钮到达时重新加载站点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:我有一个包含动态内容的网站,每次用户看到它时都需要重新加载。这包括用户点击另一个站点上的后退按钮并进入需要重新加载的站点时的用例。大多数(所有?)浏览器在此事件后不刷新网站。

Problem: I have a site with dynamic content which needs to be reloaded every time the user sees it. This includes the use case when a user hits the back button on an another site and comes to the site needed to be reloaded. Most (all?) browsers don't refresh the site after this event.

我的解决方案(不太正常):
http://www.hunlock.com/blogs/Mastering_The_Back_Button_With_Javascript

My solution (which isn't quite working): http://www.hunlock.com/blogs/Mastering_The_Back_Button_With_Javascript

window.onbeforeunload = function () {
    // This function does nothing.  It won't spawn a confirmation dialog
    // But it will ensure that the page is not cached by the browser.
}

但它仍然不刷新页面。

任何影响/阻止所需行为的想法?对于这个问题,分别有任何其他解决方案建议吗?

Any ideas what can affect/block the desired behavior? Respectively any other solution suggestions for this problem?

编辑:

设置如下:

Cache-Control   private, must-revalidate, max-age=0
Expires Sat, 26 Jul 1997 05:00:00 GMT
Pragma  no-cache

和:

<meta name="cache-control" content="no-cache" />
<meta name="expires" content="0" />
<meta name="pragma" content="no-cache" />

仍然没有成功。

推荐答案

您应该使用隐藏的输入作为刷新指示器,值为no:

You should use a hidden input as a refresh indicator, with a value of "no":

<input type="hidden" id="refresh" value="no">

现在使用jQuery,你可以检查它的价值:

Now using jQuery, you can check its value:

$(document).ready(function(e) {
    var $input = $('#refresh');

    $input.val() == 'yes' ? location.reload(true) : $input.val('yes');
});

单击后退按钮时,隐藏字段中的值保留与原始值相同的值离开页面。

When you click on the back button, the values in hidden fields retain the same value as when you originally left the page.

因此,当您第一次加载页面时,输入的值将为no。当您返回页面时,它将为是,您的JavaScript代码将触发刷新。

So the first time you load the page, the input's value would be "no". When you return to the page, it'll be "yes" and your JavaScript code will trigger a refresh.

这篇关于通过浏览器返回按钮到达时重新加载站点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆