How can I refresh a page with jQuery?

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

How can I refresh a page with jQuery?

28条回答
  •  甜味超标
    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);
    });
    

提交回复
热议问题