如何使用 JavaScript 重新加载页面

2022-08-29 22:03:34

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

我需要一个适用于所有浏览器的方法。


答案 1

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

答案 2
location.reload();

有关详细信息,请参阅此 MDN 页面

如果您在 之后刷新,则需要在 之后直接返回 falseonclick

location.reload();
return false;