Pages Navigation Menu

Coding is much easier than you think

How to Reload a Page Only Once Using Javascript

This code snippet allows reloading a page only once. It will be helpful to prevent some data being cached.

<script language="JavaScript" type="text/javascript">
var reload = false;
var loc=""+document.location;
loc = loc.indexOf("?reload=")!=-1?loc.substring(loc.indexOf("?reload=")+10,loc.length):"";
loc = loc.indexOf("&")!=-1?loc.substring(0,loc.indexOf("&")):loc;
reload = loc!=""?(loc=="true"):reload;

function reloadPage() {
    if (!reload) 
//You can call this via the body tag if desired

Another must read


About Mohaideen Jamil

Am currently working as a Struts 2 Developer in a reputed IT Organisations. I can help you with teaching Core java and Struts 2. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

  • abajanWayne

    FYI scripts no longer require the language and type attributes. Also, reloadPage(); isn’t necessary because if the function is wrapped in parentheses and an invocation operator (pair of parentheses) were to be placed right after the function’s closing brace, it would call itself. So, the following IIFE (immediately invoked function expression) could be used instead:

    (function() {
    if (!reload)
    window.location.replace(window.location + "?reload=true");

    By the way, like Mike said, the script doesn’t work as stated. It just keeps continually refreshing the page! Well, at least that’s what’s happening in Chrome.

  • Mike

    Does not work for me. Page constantly reloads – not just once. This is about the 5th time I found some JavaScript code that supposedly only resets a page one time – none of them work.

    Seems to me there should be a “flag” of some sort outside/external to the page to reset once. Maybe a cookie? I just don’t see how code on the affected page cannot help but repeatedly reset the page.

  • Eudora

    Thanks for posting this. Was looking for this info all over the web.