如何强制浏览器的页面刷新?

时间:2021-10-22 17:24:51

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"];

2 个解决方案

#1


21  

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 Page1 do something like this:

在Page1中做这样的事情:

<script>
     if (sessionStorage.getItem("Page2Visited")) {
          sessionStorage.removeItem("Page2Visited");
          window.location.reload(true); // force refresh page1
     }
</script>

And in page2:

所以page2和:

<script>
     sessionStorage.setItem("Page2Visited", "True");
</script>

Now, it will force a page refresh on page1, whenever you click back button from page2.

现在,当你从page2点击后退按钮时,它将强制页面刷新page1。

#2


2  

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);

#1


21  

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 Page1 do something like this:

在Page1中做这样的事情:

<script>
     if (sessionStorage.getItem("Page2Visited")) {
          sessionStorage.removeItem("Page2Visited");
          window.location.reload(true); // force refresh page1
     }
</script>

And in page2:

所以page2和:

<script>
     sessionStorage.setItem("Page2Visited", "True");
</script>

Now, it will force a page refresh on page1, whenever you click back button from page2.

现在,当你从page2点击后退按钮时,它将强制页面刷新page1。

#2


2  

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);