Run the codes after reoading the page in jquery

2019-05-29 15:13发布

问题:

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

function showServicesList(){
    location.reload();

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

But page is refreshing but the below codes are not running.. Any help ..

回答1:

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.