Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript page refresh in Safari

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?

like image 908
AlterWorld Avatar asked Oct 03 '11 22:10

AlterWorld


People also ask

Can JavaScript refresh a page?

How to Refresh a Page in JavaScript With location.reload() You can use the location.reload() JavaScript method to reload the current URL. This method functions similarly to the browser's Refresh button. The reload() method is the main method responsible for page reloading.

How do I refresh Safari on IOS 15?

Tap: on the Status Bar. Swipe: down to perform this page refresh.


1 Answers

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;
}
like image 111
user2979995 Avatar answered Oct 01 '22 19:10

user2979995