javascript - How to run the statement after reloading the page in jquery - Stack Overflow

I have some few lines of codes which I want to run after page reload.function showServicesList(){locati

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 ?

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 ?

Share Improve this question edited Dec 13, 2019 at 7:25 Khalid Khan 3,1851 gold badge13 silver badges29 bronze badges asked May 21, 2014 at 14:17 Sariban D'ClSariban D'Cl 2,2272 gold badges30 silver badges42 bronze badges 3
  • 2 location.reload() will immediately reload the page as if you had reloaded/refreshed it manually. Any context is lost and you're script execution is halted never making it to any other statement. – phuzi Commented May 21, 2014 at 14:19
  • Do you only want to run the rest of the code after the reload or would it be ok to run it on first load? – phuzi Commented May 21, 2014 at 14:32
  • I want to run the rest of the code after reload. – Sariban D'Cl Commented May 21, 2014 at 14:38
Add a ment  | 

1 Answer 1

Reset to default 7

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745300453a4621404.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信