How can I refresh a page with jQuery?

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

How can I refresh a page with jQuery?

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

    use

    location.reload();
    

    or

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

    There are multiple unlimited ways to refresh a page with JavaScript:

    1. location.reload()
    2. history.go(0)
    3. location.href = location.href
    4. location.href = location.pathname
    5. location.replace(location.pathname)
    6. location.reload(false)

      If we needed to pull the document from the web-server again (such as where the document contents change dynamically) we would pass the argument as true.

    You can continue the list being creative:

    • window.location = window.location
    • window.self.window.self.window.window.location = window.location
    • ...and other 534 ways

    var methods = [
      "location.reload()",
      "history.go(0)",
      "location.href = location.href",
      "location.href = location.pathname",
      "location.replace(location.pathname)",
      "location.reload(false)"
    ];
    
    var $body = $("body");
    for (var i = 0; i < methods.length; ++i) {
      (function(cMethod) {
        $body.append($("<button>", {
          text: cMethod
        }).on("click", function() {
          eval(cMethod); // don't blame me for using eval
        }));
      })(methods[i]);
    }
    button {
      background: #2ecc71;
      border: 0;
      color: white;
      font-weight: bold;
      font-family: "Monaco", monospace;
      padding: 10px;
      border-radius: 4px;
      cursor: pointer;
      transition: background-color 0.5s ease;
      margin: 2px;
    }
    button:hover {
      background: #27ae60;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    0 讨论(0)
  • 2020-11-22 07:20

    window.location.reload() will reload from the server and will load all your data, scripts, images, etc. again.

    So if you just want to refresh the HTML, the window.location = document.URL will return much quicker and with less traffic. But it will not reload the page if there is a hash (#) in the URL.

    0 讨论(0)
  • 2020-11-22 07:21

    Three approaches with different cache-related behaviours:

    • location.reload(true)

      In browsers that implement the forcedReload parameter of location.reload(), reloads by fetching a fresh copy of the page and all of its resources (scripts, stylesheets, images, etc.). Will not serve any resources from the cache - gets fresh copies from the server without sending any if-modified-since or if-none-match headers in the request.

      Equivalent to the user doing a "hard reload" in browsers where that's possible.

      Note that passing true to location.reload() is supported in Firefox (see MDN) and Internet Explorer (see MSDN) but is not supported universally and is not part of the W3 HTML 5 spec, nor the W3 draft HTML 5.1 spec, nor the WHATWG HTML Living Standard.

      In unsupporting browsers, like Google Chrome, location.reload(true) behaves the same as location.reload().

    • location.reload() or location.reload(false)

      Reloads the page, fetching a fresh, non-cached copy of the page HTML itself, and performing RFC 7234 revalidation requests for any resources (like scripts) that the browser has cached, even if they are fresh are RFC 7234 permits the browser to serve them without revalidation.

      Exactly how the browser should utilise its cache when performing a location.reload() call isn't specified or documented as far as I can tell; I determined the behaviour above by experimentation.

      This is equivalent to the user simply pressing the "refresh" button in their browser.

    • location = location (or infinitely many other possible techniques that involve assigning to location or to its properties)

      Only works if the page's URL doesn't contain a fragid/hashbang!

      Reloads the page without refetching or revalidating any fresh resources from the cache. If the page's HTML itself is fresh, this will reload the page without performing any HTTP requests at all.

      This is equivalent (from a caching perspective) to the user opening the page in a new tab.

      However, if the page's URL contains a hash, this will have no effect.

      Again, the caching behaviour here is unspecified as far as I know; I determined it by testing.

    So, in summary, you want to use:

    • location = location for maximum use of the cache, as long as the page doesn't have a hash in its URL, in which case this won't work
    • location.reload(true) to fetch new copies of all resources without revalidating (although it's not universally supported and will behave no differently to location.reload() in some browsers, like Chrome)
    • location.reload() to faithfully reproduce the effect of the user clicking the 'refresh' button.
    0 讨论(0)
  • 2020-11-22 07:21

    I found

    window.location.href = "";
    

    or

    window.location.href = null;
    

    also makes a page refresh.

    This makes it very much easier to reload the page removing any hash. This is very nice when I am using AngularJS in the iOS simulator, so that I don't have to rerun the app.

    0 讨论(0)
  • 2020-11-22 07:21

    Simple Javascript Solution:

     location = location; 
    

    <button onClick="location = location;">Reload</button>

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