Run the codes after reoading the page in jquery

房东的猫 提交于 2019-12-07 18:26:26

问题


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.



来源:https://stackoverflow.com/questions/23785894/run-the-codes-after-reoading-the-page-in-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!