如何使用JavaScript重新加载页面?

我需要一个方法,工作在所有浏览器。


当前回答

如果你把

window.location.reload(true);

在页面开始时,如果没有其他条件限定代码运行的原因,页面将加载,然后继续重新加载,直到您关闭浏览器。

其他回答

最短(更多)

history.go()

你可以使用window.location.reload();来执行这个任务。虽然有很多方法可以做到这一点,但我认为用JavaScript重新加载相同的文档是合适的方法。下面是解释

JavaScript窗口。可以使用Location对象

获取当前页面地址(URL) 将浏览器重定向到另一个页面 重新加载同一页面

window:在JavaScript中表示浏览器中打开的窗口。

location:在JavaScript中保存当前URL的信息。

location对象就像窗口对象的一个片段,通过窗口被调用。位置属性。

Location对象有三个方法:

Assign():用于加载新文档 Reload():重载当前文档 Replace():将当前文档替换为新的文档

所以这里我们需要使用reload(),因为它可以帮助我们重新加载相同的文档。

所以像window.location.reload();一样使用它。

jsfiddle上的在线演示

要要求浏览器直接从服务器而不是从缓存检索页面,可以向location.reload()传递一个true参数。该方法兼容所有主流浏览器,包括IE、Chrome、Firefox、Safari、Opera。

如果你把

window.location.reload(true);

在页面开始时,如果没有其他条件限定代码运行的原因,页面将加载,然后继续重新加载,直到您关闭浏览器。

JavaScript 1.2及更新版本

window.location.reload();
// If we needed to force the document to be fetched from the
// web server again (such as where the document contents
// change dynamically but cache control headers are not
// configured properly), Firefox supports a non-standard
// parameter that can be set to true to bypass the cache:
//window.location.reload(true);

JavaScript 1.1

window.location.replace(window.location.pathname + window.location.search + window.location.hash);
// does not create a history entry

JavaScript 1.0

window.location.href = window.location.pathname + window.location.search + window.location.hash;
// creates a history entry

Try:

window.location.reload(true);

参数设置为'true'从服务器重新加载一个新的副本。省略它将从缓存中提供页面。

更多信息可以在MSDN和Mozilla文档中找到。