javascript page refresh in Safari

2019-05-07 11:59发布

问题:

I am trying figure out how to refresh page in Safari (5.1) using javascript and nothing seems to work.

So far, I have tried,

  • window.location.href = window.location.href
  • window.location = window.location.href
  • window.location.reload(true)
  • window.location.replace(window.location.href)

What is the right way of handling page refresh in Safari?

回答1:

location.reload(true); // works for safari

If you didn't know already this site, let have a look on it, you will have a lot of example for refreshing page: http://www.quackit.com/javascript/javascript_refresh_page.cfm



回答2:

You should always use the reload() method from the location object...

window.location.reload();

Set the first argument to true if you want to hard reload (send a new GET request for the page).



回答3:

Apparently, Safari on Mac or iOS has a bug with location.reload, so, I've developed this simple cross browser solution taking advantage of the url query string:

function refresh() {
  var url = location.origin;
  var pathname = location.pathname;
  var hash = location.hash;

  location = url + pathname + '?application_refresh=' + (Math.random() * 100000) + hash;
}