How to reload page once using JQuery?

This will do it:

if (window.location.href.indexOf('reload')==-1) {
     window.location.replace(window.location.href+'?reload');
}

With your jQuery-Code:

$( window ).load(function() {
    if (window.location.href.indexOf('reload')==-1) {
         window.location.replace(window.location.href+'?reload');
    }
});

Nowadays... HTML5 provides localStorage:

/* if my "reload" var isn't set locally.. getItem will be false */
if (!localStorage.getItem("reload")) {
    /* set reload to true and then reload the page */
    localStorage.setItem("reload", "true");
    location.reload();
}
/* after reloading remove "reload" from localStorage */
else {
    localStorage.removeItem("reload");
    // localStorage.clear(); // or clear it, instead
}

and this doesn't need jquery. Hope it helps, as it helped me.


<script type="text/javascript">
$(document).ready(function(){

    //Check if the current URL contains '#'
    if(document.URL.indexOf("#")==-1)
    {
    // Set the URL to whatever it was plus "#".
    url = document.URL+"#";
    location = "#";

    //Reload the page
    location.reload(true);

    }
    });
</script>

Due to the if condition the page will reload only once.I faced this problem too and when I search ,I found a nice solution. This works for me fine.And thanks for http://www.webdeveloper.com/forum/showthread.php?267853-Auto-refresh-page-once-only-after-first-load

Tags:

Jquery