how to force page refresh on browser back click?

前端 未结 2 1414
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-10 14:45

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 :

i have

相关标签:
2条回答
  • 2020-12-10 15:24

    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

    In Page1 do something like this:

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

    And in page2:

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

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

    0 讨论(0)
  • 2020-12-10 15:27

    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);
    
    0 讨论(0)
提交回复
热议问题