Safari 中的 javascript 页面刷新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7641584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:46:40  来源:igfitidea点击:

javascript page refresh in Safari

javascriptsafari

提问by AlterWorld

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

我正在尝试弄清楚如何使用 javascript 在 Safari (5.1) 中刷新页面,但似乎没有任何效果。

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)
  • 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?

在 Safari 中处理页面刷新的正确方法是什么?

回答by Guilhem Hoffmann

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

如果你还不知道这个网站,让我们看看它,你会有很多刷新页面的例子:http: //www.quackit.com/javascript/javascript_refresh_page.cfm

回答by user2979995

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:

显然,Mac 或 iOS 上的 Safari 存在 location.reload 的错误,因此,我利用 url 查询字符串开发了这个简单的跨浏览器解决方案:

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

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

回答by alex

You should always use the reload()method from the locationobject...

您应该始终使用对象中的reload()方法location...

window.location.reload();

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

如果要硬重新加载(为页面发送新的 GET 请求),请将第一个参数设置为 true 。