Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to force page refresh on browser back click?

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 a js setcookie function that record changes in a type selector

$( "#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

 if (isset($_COOKIE["liste-voyage-type"]))
        $type=$_COOKIE["liste-voyage-type"];
like image 483
Matoeil Avatar asked Nov 13 '13 12:11

Matoeil


2 Answers

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.

like image 116
Shibbir Ahmed Avatar answered Nov 09 '22 17:11

Shibbir Ahmed


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);
like image 36
Matoeil Avatar answered Nov 09 '22 19:11

Matoeil