window.location.reload complete?

13,380

Solution 1

You simply have to provide a function to onload : a reload isn't different from a load.

window.onload = function() { // ...

Solution 2

Your line of code window.location.reload(false) causes the browser to reload the current page - no script after this statement will be executed ....

You could set a cookie on first load - then on subsequent loads check for the existence of the cookie and perform an action ... pseudo code :

onload = check cookie
if cookie is present
   run function
else
   set cookie
   reload

You could check the time on the cookie and choose to execute the function after a period of time (1 hour for example) has passed ....

Solution 3

I use the hashtag to set variables that tells me if the page is reloaded or not. You could do something like this:

// Get the hash of the page
var hashstring = window.location.hash.substring(1);
var found = false;

// Do a hash exist?
if (hashstring.length > 0) 
{
    // Split the hash by '&'-sign (in case you have more variables in the hash, as I have)
    var a = hashstring.split("&");

    // Loop through the values
    for (i = 0; i < a.length; i++)
    {
        // Split the string by '=' (key=value format)
        var b = a[i].split("=");

        // If the key is 'reloaded' (which tells us if the page is reloaded)
        if(b[0] == 'reloaded')
        {
            found = true;
        }
    }
}    

if(!found)
{
    location.hash = 'reloaded=true';
    window.location.reload();
}

// Do other stuff, this will only be executed if the page has been reloaded

I've put the code that finds a variable in the hash in a seperate function in my project, but fot simplicity I just added it here above. This makes it possible to determine if the page has been reloaded, and run code only if it has.

Share:
13,380
user840930
Author by

user840930

Updated on June 06, 2022

Comments

  • user840930
    user840930 almost 2 years

    I call

    window.location.reload(false) 
    

    in a javascript method to update the page. I have additional javascript calls after this call. Is there a way to know when window.location.reload(false) has completed running before calling the additional javascript calls?