如何使用 JavaScript 重新加载页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3715047/
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-08-23 05:49:48  来源:igfitidea点击:

How to reload a page using JavaScript

javascriptreload

提问by Resh

How can I reload the page using JavaScript?

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

I need a method that works in all browsers.

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

回答by gianebao

JavaScript 1.0

JavaScript 1.0

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

JavaScript 1.1

JavaScript 1.1

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

JavaScript 1.2

JavaScript 1.2

window.location.reload(false); 
// If we needed to pull the document from
//  the web-server again (such as where the document contents
//  change dynamically) we would pass the argument as 'true'.

回答by Lekensteyn

location.reload();

See this MDN pagefor more information.

有关更多信息,请参阅此MDN 页面

If you are refreshing after an onclickthen you'll need to return false directly after

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

location.reload();
return false;

回答by Jeremy Thille

Here are 535 ways to reload the page using JavaScript, the easiest being location = location.

这里有535 种使用 JavaScript 重新加载页面的方法,最简单的是location = location.

These are the first 50:

这些是前 50 个:

location = location
location = location.href
location = window.location
location = self.location
location = window.location.href
location = self.location.href
location = location['href']
location = window['location']
location = window['location'].href
location = window['location']['href']
location = window.location['href']
location = self['location']
location = self['location'].href
location = self['location']['href']
location = self.location['href']
location.assign(location)
location.replace(location)
window.location.assign(location)
window.location.replace(location)
self.location.assign(location)
self.location.replace(location)
location['assign'](location)
location['replace'](location)
window.location['assign'](location)
window.location['replace'](location)
window['location'].assign(location)
window['location'].replace(location)
window['location']['assign'](location)
window['location']['replace'](location)
self.location['assign'](location)
self.location['replace'](location)
self['location'].assign(location)
self['location'].replace(location)
self['location']['assign'](location)
self['location']['replace'](location)
location.href = location
location.href = location.href
location.href = window.location
location.href = self.location
location.href = window.location.href
location.href = self.location.href
location.href = location['href']
location.href = window['location']
location.href = window['location'].href
location.href = window['location']['href']
location.href = window.location['href']
location.href = self['location']
location.href = self['location'].href
location.href = self['location']['href']
location.href = self.location['href']
...

回答by Nikhil Agrawal

You can perform this task using window.location.reload();. As there are many ways to do this but I think it is the appropriate way to reload the same document with JavaScript. Here is the explanation

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

JavaScript window.locationobject can be used

window.location可以使用JavaScript对象

  • to get current page address (URL)
  • to redirect the browser to another page
  • to reload the same page
  • 获取当前页面地址(URL)
  • 将浏览器重定向到另一个页面
  • 重新加载同一页面

window: in JavaScript represents an open window in a browser.

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

location: in JavaScript holds information about current URL.

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

The locationobject is like a fragment of the windowobject and is called up through the window.locationproperty.

location对象是像的片段window对象,并通过调用window.location属性。

locationobject has three methods:

location对象有三个方法:

  1. assign(): used to load a new document
  2. reload(): used to reload current document
  3. replace(): used to replace current document with a new one
  1. assign(): 用于加载新文档
  2. reload(): 用于重新加载当前文档
  3. replace(): 用于用新文档替换当前文档

So here we need to use reload(), because it can help us in reloading the same document.

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

So use it like window.location.reload();.

所以像window.location.reload();.

Online demo on jsfiddle

jsfiddle 在线演示

To ask your browser to retrieve the page directly from the server not from the cache, you can pass a trueparameter to location.reload(). This method is compatible with all major browsers, including IE, Chrome, Firefox, Safari, Opera.

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

回答by Orane

Try:

尝试:

window.location.reload(true);

The parameter set to 'true' reloads a fresh copy from the server. Leaving it out will serve the page from cache.

设置为“true”的参数会从服务器重新加载新副本。离开它将从缓存中提供页面。

More information can be found at MSDNand in the Mozilla documentation.

更多信息可以在MSDNMozilla 文档中找到

回答by Starwarswii

I was looking for some information regarding reloads on pages retrieved with POST requests, such as after submitting a method="post"form.

我一直在寻找有关使用 POST 请求检索的页面上重新加载的一些信息,例如在提交method="post"表单之后。

To reload the page keeping the POST data,use:

要重新加载保留 POST 数据的页面,请使用:

window.location.reload();

window.location.reload();

To reload the page discarding the POST data (perform a GET request),use:

要重新加载页面并丢弃 POST 数据(执行 GET 请求),请使用:

window.location.href = window.location.href;

window.location.href = window.location.href;

Hopefully this can help others looking for the same information.

希望这可以帮助其他人寻找相同的信息。

回答by Girish Ninama

To reload a page using JavaScript, use:

要使用 JavaScript 重新加载页面,请使用:

window.location.reload();

回答by umer

This works for me:

这对我有用:

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

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

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

回答by Joe Devlin

If you put

如果你把

window.location.reload(true);

at the beginning of your page with no other condition qualifying why that code runs, the page will load and then continue to reload itself until you close your browser.

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

回答by u443966

location.href = location.href;