How can I refresh a page with jQuery?

后端 未结 28 2444
刺人心
刺人心 2020-11-22 07:00

How can I refresh a page with jQuery?

相关标签:
28条回答
  • 2020-11-22 07:22

    Use onclick="return location.reload();" within the button tag.

    <button id="refersh-page" name="refersh-page" type="button" onclick="return location.reload();">Refesh Page</button>
    
    0 讨论(0)
  • 2020-11-22 07:24

    Lots of ways will work, I suppose:

    • window.location.reload();
    • history.go(0);
    • window.location.href=window.location.href;
    0 讨论(0)
  • 2020-11-22 07:25

    Probably shortest (12 chars) - use history

    history.go()
    
    0 讨论(0)
  • 2020-11-22 07:26

    As the question is generic, let's try to sum up possible solutions for the answer:

    Simple plain JavaScript Solution:

    The easiest way is a one line solution placed in an appropriate way:

    location.reload();
    

    What many people are missing here, because they hope to get some "points" is that the reload() function itself offers a Boolean as a parameter (details: https://developer.mozilla.org/en-US/docs/Web/API/Location/reload).

    The Location.reload() method reloads the resource from the current URL. Its optional unique parameter is a Boolean, which, when it is true, causes the page to always be reloaded from the server. If it is false or not specified, the browser may reload the page from its cache.

    This means there are two ways:

    Solution1: Force reloading the current page from the server

    location.reload(true);
    

    Solution2: Reloading from cache or server (based on browser and your config)

    location.reload(false);
    location.reload();
    

    And if you want to combine it with jQuery an listening to an event, I would recommend using the ".on()" method instead of ".click" or other event wrappers, e.g. a more proper solution would be:

    $('#reloadIt').on('eventXyZ', function() {
        location.reload(true);
    });
    
    0 讨论(0)
  • 2020-11-22 07:28

    This should work on all browsers even without jQuery:

    location.reload();
    
    0 讨论(0)
  • 2020-11-22 07:29

    You may want to use

    location.reload(forceGet)
    

    forceGet is a boolean and optional.

    The default is false which reloads the page from the cache.

    Set this parameter to true if you want to force the browser to get the page from the server to get rid of the cache as well.

    Or just

    location.reload()
    

    if you want quick and easy with caching.

    0 讨论(0)
提交回复
热议问题