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

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


当前回答

这应该可以工作:

window.location.href = window.location.href.split( '#' )[0];

or

var x = window.location.href;
x = x.split( '#' );
window.location.href = x[0];

我更喜欢这个,原因如下:

删除#后面的部分,确保页面在浏览器上重新加载,而不会重新加载包含该内容的内容。 如果你最近提交了一个表单,它不会询问你是否想要重新发布上一个内容。 它甚至可以在最新的浏览器上运行。在持续的Firefox和Chrome上测试。

或者,您可以使用最新的官方方法完成此任务

window.location.reload()

其他回答

这对我来说很管用:

function refresh() {    
    setTimeout(function () {
        location.reload()
    }, 100);
}

http://jsfiddle.net/umerqureshi/znruyzop/

如果你把

window.location.reload(true);

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

Javascript reload()方法用于重新加载当前文档或URL。javascript的location.reload(true)方法就像浏览器中的reload按钮一样工作。默认情况下,JS的reload()方法从缓存中重新加载页面,但是你可以通过将forceGet参数设置为true: location来强制它从服务器端重新加载页面。重载(真正的)。 来源:https://www.coderepublics.com/JavaScript/javascript-location-reload-true.php

deprated怎么样? 它只是用forcedReload重新加载,现在已弃用。但是为了避免被放弃的错误,你可以使用location.reload()而不带forcerload标志。

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
location.href = location.href;