javascript 如何在浏览器后退点击时强制刷新页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19953382/
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
how to force page refresh on browser back click?
提问by Matoeil
Is there a cross-browser compatible way of forcing page refresh when clicking the navigator back button?
单击导航器后退按钮时,是否有强制刷新页面的跨浏览器兼容方式?
i am trying to access to actualised cookies :
我正在尝试访问已实现的 cookie:
i have a js setcookie function that record changes in a type selector
我有一个 js setcookie 函数,用于记录类型选择器中的更改
$( "#type-select" ).change(function() {
var type = $("#type-select").val();
SetCookie("liste-voyage-type",type);
});
i'd like to retrieve that value when returning on this page, clicking the browser back button, using php
我想在返回此页面时检索该值,单击浏览器后退按钮,使用 php
if (isset($_COOKIE["liste-voyage-type"]))
$type=$_COOKIE["liste-voyage-type"];
回答by Shibbir Ahmed
I had a similar requirement in my project. You can do something like this:
我在我的项目中有类似的要求。你可以这样做:
For example, lets say there are two pages: page1 and page2
例如,假设有两个页面:page1 和 page2
In Page1do something like this:
在Page1 中做这样的事情:
<script>
if (sessionStorage.getItem("Page2Visited")) {
sessionStorage.removeItem("Page2Visited");
window.location.reload(true); // force refresh page1
}
</script>
And in page2:
而在第2页:
<script>
sessionStorage.setItem("Page2Visited", "True");
</script>
Now, it will force a page refresh on page1, whenever you click back button from page2.
现在,每当您单击 page2 的后退按钮时,它将强制在 page1 上刷新页面。
回答by Matoeil
i did it slighty differently with cookies
我用饼干做的有点不同
function SetCookie (name, value) {
var argv=SetCookie.arguments;
var argc=SetCookie.arguments.length;
var expires=(argc > 2) ? argv[2] : null;
var path=(argc > 3) ? argv[3] : null;
var domain=(argc > 4) ? argv[4] : null;
var secure=(argc > 5) ? argv[5] : false;
document.cookie=name+"="+escape(value)+
((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
((path==null) ? "" : ("; path="+path))+
((domain==null) ? "" : ("; domain="+domain))+
((secure==true) ? "; secure" : "");
}
function getCookie(c_name)
{
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
{
c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1)
{
c_value = null;
}
else
{
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1)
{
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
}
if (getCookie('first_load'))
{
if (getCookie('first_load')==true)
{
window.location.reload(true); // force refresh page-liste
SetCookie("first_load",false);
}
}
SetCookie("first_load",true);