Search code examples
javascriptjqueryreload

How to run the statement after reloading the page in jquery


I have some few lines of codes which I want to run after page reload.

function showServicesList(){
    location.reload();

    /*
    *   Statement1
    *   Statement2
    *   Statement3
    *   Statement4
    */
}   

Page is refreshing, but the statement below after location.reload doesn't execute. Any help in this ?


Solution

  • Well. If you reload your page, any JavaScript is stopped and reloaded too. If you want your code to run AFTER the page is reloaded you could add a hash.

    window.location.hash = "triggerReloadCode";
    window.location.reload();
    
    if (window.location.hash.substr(1) == "triggerReloadCode") {
        window.location.hash = "";
        /* Statements */
    }
    

    I don't know if that's best practice, but I would trigger it like this.