javascript - How can I refresh a page with jQuery?

ID : 270

viewed : 337

Tags : javascriptjqueryrefreshreloadjavascriptjavascriptjavascript





Top 5 Answer for javascript - How can I refresh a page with jQuery?

vote vote

96

Use location.reload():

$('#something').click(function() {     location.reload(); }); 

The reload() function takes an optional parameter that can be set to true to force a reload from the server rather than the cache. The parameter defaults to false, so by default the page may reload from the browser's cache.

vote vote

84

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>

vote vote

73

This should work on all browsers even without jQuery:

location.reload(); 
vote vote

66

Lots of ways will work, I suppose:

  • window.location.reload();
  • history.go(0);
  • window.location.href=window.location.href;
vote vote

51

To reload a page with jQuery, do:

$.ajax({     url: "",     context: document.body,     success: function(s,x){         $(this).html(s);     } }); 

The approach here that I used was Ajax jQuery. I tested it on Chrome 13. Then I put the code in the handler that will trigger the reload. The URL is "", which means this page.

Top 3 video Explaining javascript - How can I refresh a page with jQuery?







Related QUESTION?