How to reload a page using JavaScript

前端 未结 19 2620
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 00:46

How can I reload the page using JavaScript?

I need a method that works in all browsers.

19条回答
  •  时光说笑
    2020-11-22 01:47

    This should work:

    window.location.href = window.location.href.split( '#' )[0];
    

    or

    var x = window.location.href;
    x = x.split( '#' );
    window.location.href = x[0];
    

    I prefer this for the following reasons:

    • Removes the part after the #, ensuring the page reloads on browsers that won't reload content that has it.
    • It doesn't ask you if want to repost last content if you recently submit a form.
    • It should work even on most recent browsers. Tested on Lasted Firefox and Chrome.

    Alternatively, you may use the most recent official method for this task

    window.location.reload()
    

提交回复
热议问题