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

我如何使用JavaScript重新加载页面? 我需要一个适用于所有浏览器的方法。

Javascript 1.0

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

Javascript 1.1

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

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'. 
 location.reload(); 

有关location对象的更多信息,请参阅此MDC页面 。

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

可以使用Javascript的window.location对象

  • 获取当前页面地址(URL)。
  • 将浏览器redirect到其他页面。
  • 重新加载相同的页面。

窗口 – Javascript中的窗口对象表示浏览器中的打开窗口。

位置 – Javascript中的位置对象包含有关当前URL的信息。

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

位置对象有三个方法 –

  1. assign()用于加载新文档
  2. reload()用于重新加载当前文档。
  3. replace()用于用新的replace当前文档

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

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

在线演示jsfiddle

要让浏览器直接从服务器中检索页面,而不是从caching中检索,可以传递一个true参数给location.reload():

此方法与所有主stream浏览器兼容

  • IE,Chrome,Firefox,Safari,Opera。

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

这是第一个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'] ... 

尝试:

 window.location.reload(true); 

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

更多信息可以在MSDN和Mozilla文档中find

这项工作对我来说是:

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

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

使用JavaScript重新加载页面的其他500种方法

 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'] location.assign(location.href) location.replace(location.href) window.location.assign(location.href) window.location.replace(location.href) self.location.assign(location.href) self.location.replace(location.href) location['assign'](location.href) location['replace'](location.href) window.location['assign'](location.href) window.location['replace'](location.href) window['location'].assign(location.href) window['location'].replace(location.href) window['location']['assign'](location.href) window['location']['replace'](location.href) self.location['assign'](location.href) self.location['replace'](location.href) self['location'].assign(location.href) self['location'].replace(location.href) self['location']['assign'](location.href) self['location']['replace'](location.href) window.location = location window.location = location.href window.location = window.location window.location = self.location window.location = window.location.href window.location = self.location.href window.location = location['href'] window.location = window['location'] window.location = window['location'].href window.location = window['location']['href'] window.location = window.location['href'] window.location = self['location'] window.location = self['location'].href window.location = self['location']['href'] window.location = self.location['href'] location.assign(window.location) location.replace(window.location) window.location.assign(window.location) window.location.replace(window.location) self.location.assign(window.location) self.location.replace(window.location) location['assign'](window.location) location['replace'](window.location) window.location['assign'](window.location) window.location['replace'](window.location) window['location'].assign(window.location) window['location'].replace(window.location) window['location']['assign'](window.location) window['location']['replace'](window.location) self.location['assign'](window.location) self.location['replace'](window.location) self['location'].assign(window.location) self['location'].replace(window.location) self['location']['assign'](window.location) self['location']['replace'](window.location) self.location = location self.location = location.href self.location = window.location self.location = self.location self.location = window.location.href self.location = self.location.href self.location = location['href'] self.location = window['location'] self.location = window['location'].href self.location = window['location']['href'] self.location = window.location['href'] self.location = self['location'] self.location = self['location'].href self.location = self['location']['href'] self.location = self.location['href'] location.assign(self.location) location.replace(self.location) window.location.assign(self.location) window.location.replace(self.location) self.location.assign(self.location) self.location.replace(self.location) location['assign'](self.location) location['replace'](self.location) window.location['assign'](self.location) window.location['replace'](self.location) window['location'].assign(self.location) window['location'].replace(self.location) window['location']['assign'](self.location) window['location']['replace'](self.location) self.location['assign'](self.location) self.location['replace'](self.location) self['location'].assign(self.location) self['location'].replace(self.location) self['location']['assign'](self.location) self['location']['replace'](self.location) window.location.href = location window.location.href = location.href window.location.href = window.location window.location.href = self.location window.location.href = window.location.href window.location.href = self.location.href window.location.href = location['href'] window.location.href = window['location'] window.location.href = window['location'].href window.location.href = window['location']['href'] window.location.href = window.location['href'] window.location.href = self['location'] window.location.href = self['location'].href window.location.href = self['location']['href'] window.location.href = self.location['href'] location.assign(window.location.href) location.replace(window.location.href) window.location.assign(window.location.href) window.location.replace(window.location.href) self.location.assign(window.location.href) self.location.replace(window.location.href) location['assign'](window.location.href) location['replace'](window.location.href) window.location['assign'](window.location.href) window.location['replace'](window.location.href) window['location'].assign(window.location.href) window['location'].replace(window.location.href) window['location']['assign'](window.location.href) window['location']['replace'](window.location.href) self.location['assign'](window.location.href) self.location['replace'](window.location.href) self['location'].assign(window.location.href) self['location'].replace(window.location.href) self['location']['assign'](window.location.href) self['location']['replace'](window.location.href) self.location.href = location self.location.href = location.href self.location.href = window.location self.location.href = self.location self.location.href = window.location.href self.location.href = self.location.href self.location.href = location['href'] self.location.href = window['location'] self.location.href = window['location'].href self.location.href = window['location']['href'] self.location.href = window.location['href'] self.location.href = self['location'] self.location.href = self['location'].href self.location.href = self['location']['href'] self.location.href = self.location['href'] location.assign(self.location.href) location.replace(self.location.href) window.location.assign(self.location.href) window.location.replace(self.location.href) self.location.assign(self.location.href) self.location.replace(self.location.href) location['assign'](self.location.href) location['replace'](self.location.href) window.location['assign'](self.location.href) window.location['replace'](self.location.href) window['location'].assign(self.location.href) window['location'].replace(self.location.href) window['location']['assign'](self.location.href) window['location']['replace'](self.location.href) self.location['assign'](self.location.href) self.location['replace'](self.location.href) self['location'].assign(self.location.href) self['location'].replace(self.location.href) self['location']['assign'](self.location.href) self['location']['replace'](self.location.href) 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'] location.assign(location['href']) location.replace(location['href']) window.location.assign(location['href']) window.location.replace(location['href']) self.location.assign(location['href']) self.location.replace(location['href']) location['assign'](location['href']) location['replace'](location['href']) window.location['assign'](location['href']) window.location['replace'](location['href']) window['location'].assign(location['href']) window['location'].replace(location['href']) window['location']['assign'](location['href']) window['location']['replace'](location['href']) self.location['assign'](location['href']) self.location['replace'](location['href']) self['location'].assign(location['href']) self['location'].replace(location['href']) self['location']['assign'](location['href']) self['location']['replace'](location['href']) window['location'] = location window['location'] = location.href window['location'] = window.location window['location'] = self.location window['location'] = window.location.href window['location'] = self.location.href window['location'] = location['href'] window['location'] = window['location'] window['location'] = window['location'].href window['location'] = window['location']['href'] window['location'] = window.location['href'] window['location'] = self['location'] window['location'] = self['location'].href window['location'] = self['location']['href'] window['location'] = self.location['href'] location.assign(window['location']) location.replace(window['location']) window.location.assign(window['location']) window.location.replace(window['location']) self.location.assign(window['location']) self.location.replace(window['location']) location['assign'](window['location']) location['replace'](window['location']) window.location['assign'](window['location']) window.location['replace'](window['location']) window['location'].assign(window['location']) window['location'].replace(window['location']) window['location']['assign'](window['location']) window['location']['replace'](window['location']) self.location['assign'](window['location']) self.location['replace'](window['location']) self['location'].assign(window['location']) self['location'].replace(window['location']) self['location']['assign'](window['location']) self['location']['replace'](window['location']) window['location'].href = location window['location'].href = location.href window['location'].href = window.location window['location'].href = self.location window['location'].href = window.location.href window['location'].href = self.location.href window['location'].href = location['href'] window['location'].href = window['location'] window['location'].href = window['location'].href window['location'].href = window['location']['href'] window['location'].href = window.location['href'] window['location'].href = self['location'] window['location'].href = self['location'].href window['location'].href = self['location']['href'] window['location'].href = self.location['href'] location.assign(window['location'].href) location.replace(window['location'].href) window.location.assign(window['location'].href) window.location.replace(window['location'].href) self.location.assign(window['location'].href) self.location.replace(window['location'].href) location['assign'](window['location'].href) location['replace'](window['location'].href) window.location['assign'](window['location'].href) window.location['replace'](window['location'].href) window['location'].assign(window['location'].href) window['location'].replace(window['location'].href) window['location']['assign'](window['location'].href) window['location']['replace'](window['location'].href) self.location['assign'](window['location'].href) self.location['replace'](window['location'].href) self['location'].assign(window['location'].href) self['location'].replace(window['location'].href) self['location']['assign'](window['location'].href) self['location']['replace'](window['location'].href) window['location']['href'] = location window['location']['href'] = location.href window['location']['href'] = window.location window['location']['href'] = self.location window['location']['href'] = window.location.href window['location']['href'] = self.location.href window['location']['href'] = location['href'] window['location']['href'] = window['location'] window['location']['href'] = window['location'].href window['location']['href'] = window['location']['href'] window['location']['href'] = window.location['href'] window['location']['href'] = self['location'] window['location']['href'] = self['location'].href window['location']['href'] = self['location']['href'] window['location']['href'] = self.location['href'] location.assign(window['location']['href']) location.replace(window['location']['href']) window.location.assign(window['location']['href']) window.location.replace(window['location']['href']) self.location.assign(window['location']['href']) self.location.replace(window['location']['href']) location['assign'](window['location']['href']) location['replace'](window['location']['href']) window.location['assign'](window['location']['href']) window.location['replace'](window['location']['href']) window['location'].assign(window['location']['href']) window['location'].replace(window['location']['href']) window['location']['assign'](window['location']['href']) window['location']['replace'](window['location']['href']) self.location['assign'](window['location']['href']) self.location['replace'](window['location']['href']) self['location'].assign(window['location']['href']) self['location'].replace(window['location']['href']) self['location']['assign'](window['location']['href']) self['location']['replace'](window['location']['href']) window.location['href'] = location window.location['href'] = location.href window.location['href'] = window.location window.location['href'] = self.location window.location['href'] = window.location.href window.location['href'] = self.location.href window.location['href'] = location['href'] window.location['href'] = window['location'] window.location['href'] = window['location'].href window.location['href'] = window['location']['href'] window.location['href'] = window.location['href'] window.location['href'] = self['location'] window.location['href'] = self['location'].href window.location['href'] = self['location']['href'] window.location['href'] = self.location['href'] location.assign(window.location['href']) location.replace(window.location['href']) window.location.assign(window.location['href']) window.location.replace(window.location['href']) self.location.assign(window.location['href']) self.location.replace(window.location['href']) location['assign'](window.location['href']) location['replace'](window.location['href']) window.location['assign'](window.location['href']) window.location['replace'](window.location['href']) window['location'].assign(window.location['href']) window['location'].replace(window.location['href']) window['location']['assign'](window.location['href']) window['location']['replace'](window.location['href']) self.location['assign'](window.location['href']) self.location['replace'](window.location['href']) self['location'].assign(window.location['href']) self['location'].replace(window.location['href']) self['location']['assign'](window.location['href']) self['location']['replace'](window.location['href']) self['location'] = location self['location'] = location.href self['location'] = window.location self['location'] = self.location self['location'] = window.location.href self['location'] = self.location.href self['location'] = location['href'] self['location'] = window['location'] self['location'] = window['location'].href self['location'] = window['location']['href'] self['location'] = window.location['href'] self['location'] = self['location'] self['location'] = self['location'].href self['location'] = self['location']['href'] self['location'] = self.location['href'] location.assign(self['location']) location.replace(self['location']) window.location.assign(self['location']) window.location.replace(self['location']) self.location.assign(self['location']) self.location.replace(self['location']) location['assign'](self['location']) location['replace'](self['location']) window.location['assign'](self['location']) window.location['replace'](self['location']) window['location'].assign(self['location']) window['location'].replace(self['location']) window['location']['assign'](self['location']) window['location']['replace'](self['location']) self.location['assign'](self['location']) self.location['replace'](self['location']) self['location'].assign(self['location']) self['location'].replace(self['location']) self['location']['assign'](self['location']) self['location']['replace'](self['location']) self['location'].href = location self['location'].href = location.href self['location'].href = window.location self['location'].href = self.location self['location'].href = window.location.href self['location'].href = self.location.href self['location'].href = location['href'] self['location'].href = window['location'] self['location'].href = window['location'].href self['location'].href = window['location']['href'] self['location'].href = window.location['href'] self['location'].href = self['location'] self['location'].href = self['location'].href self['location'].href = self['location']['href'] self['location'].href = self.location['href'] location.assign(self['location'].href) location.replace(self['location'].href) window.location.assign(self['location'].href) window.location.replace(self['location'].href) self.location.assign(self['location'].href) self.location.replace(self['location'].href) location['assign'](self['location'].href) location['replace'](self['location'].href) window.location['assign'](self['location'].href) window.location['replace'](self['location'].href) window['location'].assign(self['location'].href) window['location'].replace(self['location'].href) window['location']['assign'](self['location'].href) window['location']['replace'](self['location'].href) self.location['assign'](self['location'].href) self.location['replace'](self['location'].href) self['location'].assign(self['location'].href) self['location'].replace(self['location'].href) self['location']['assign'](self['location'].href) self['location']['replace'](self['location'].href) self['location']['href'] = location self['location']['href'] = location.href self['location']['href'] = window.location self['location']['href'] = self.location self['location']['href'] = window.location.href self['location']['href'] = self.location.href self['location']['href'] = location['href'] self['location']['href'] = window['location'] self['location']['href'] = window['location'].href self['location']['href'] = window['location']['href'] self['location']['href'] = window.location['href'] self['location']['href'] = self['location'] self['location']['href'] = self['location'].href self['location']['href'] = self['location']['href'] self['location']['href'] = self.location['href'] location.assign(self['location']['href']) location.replace(self['location']['href']) window.location.assign(self['location']['href']) window.location.replace(self['location']['href']) self.location.assign(self['location']['href']) self.location.replace(self['location']['href']) location['assign'](self['location']['href']) location['replace'](self['location']['href']) window.location['assign'](self['location']['href']) window.location['replace'](self['location']['href']) window['location'].assign(self['location']['href']) window['location'].replace(self['location']['href']) window['location']['assign'](self['location']['href']) window['location']['replace'](self['location']['href']) self.location['assign'](self['location']['href']) self.location['replace'](self['location']['href']) self['location'].assign(self['location']['href']) self['location'].replace(self['location']['href']) self['location']['assign'](self['location']['href']) self['location']['replace'](self['location']['href']) self.location['href'] = location self.location['href'] = location.href self.location['href'] = window.location self.location['href'] = self.location self.location['href'] = window.location.href self.location['href'] = self.location.href self.location['href'] = location['href'] self.location['href'] = window['location'] self.location['href'] = window['location'].href self.location['href'] = window['location']['href'] self.location['href'] = window.location['href'] self.location['href'] = self['location'] self.location['href'] = self['location'].href self.location['href'] = self['location']['href'] self.location['href'] = self.location['href'] location.assign(self.location['href']) location.replace(self.location['href']) window.location.assign(self.location['href']) window.location.replace(self.location['href']) self.location.assign(self.location['href']) self.location.replace(self.location['href']) location['assign'](self.location['href']) location['replace'](self.location['href']) window.location['assign'](self.location['href']) window.location['replace'](self.location['href']) window['location'].assign(self.location['href']) window['location'].replace(self.location['href']) window['location']['assign'](self.location['href']) window['location']['replace'](self.location['href']) self.location['assign'](self.location['href']) self.location['replace'](self.location['href']) self['location'].assign(self.location['href']) self['location'].replace(self.location['href']) self['location']['assign'](self.location['href']) self['location']['replace'](self.location['href']) location.reload() location['reload']() window.location.reload() window['location'].reload() window.location['reload']() window['location']['reload']() self.location.reload() self['location'].reload() self.location['reload']() self['location']['reload']() 

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

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

window.location.reload();

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

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

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

如果你放

 window.location.reload(true); 

在页面的开始处,没有其他条件限定代码运行的原因,页面将加载,然后继续自行重新加载,直到closures浏览器为止。

Using A button or just put inside "a(anchor)" tag

 <input type="button" value="RELOAD" onclick="location.reload();" /> 

Try these for other needs

 Location Objects has three methods -- assign() Used to load a new document reload() Used to reloads the current document. replace() Used to replace the current document with a new one 

To make it easy and simple, use location.reload() . You can also use location.reload(true) if you want to grab something from the server.

 location.href = location.href; 

Thank you, this post was very helpful not only to reload the page with the suggested answer but as well gave me an idea to place a jQuery UI icon to a button. Hope it helps someone else!

 <button style="display:block; vertical-align:middle; height:2.82em;" title="Cargar nuevamente el código fuente sin darle un [Enter] a la dirección en la barra de direcciones" class="ui-state-active ui-corner-all ui-priority-primary" onclick="javascript:window.location.reload(true);"> <span style="display:inline-block;" class="ui-icon ui-icon-refresh"></span> &nbsp;[<b>CARGAR NUEVAMENTE</b>]&nbsp; </button> 

Edited to show how this looks like when included in a project

2/7/2016

2 [ My apologies as this is a personal project that implies using jQuery UI, Themeroller, Icons framework, methods like jQuery tabs, but it's in Spanish ;) ] 2

Automatic reload page after 20 seconds.

 <script> window.onload = function() { setTimeout(function () { location.reload() }, 20000); }; </script> 

这应该工作:

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

要么

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

I prefer this for the following reasons:

  • Removes the part after the #, ensuring the page reloads on browsers that won't reload content that has it.
  • It doesn't ask you if want to repost last content if you recently submit a form.
  • It should work even on most recent browsers. Tested on Lasted Firefox and Chrome.

Alternatively, you may use the most recent official method for this task

 window.location.reload() 

Use this button to refresh the page

DEMO

 <input type="button" value="Reload Page" onClick="document.location.reload(true)"> 

你可以简单地使用

 window.location=document.URL 

where document.URL gets the current page URL and window.location reloads it.

I know all answers above will do the trick in most cases but a very simple and basic cross browser workaround would be to avoid complexity of browser specific tricks only by creating an anchor link <a> tag.

For jQuery users:

 var hiddenReloadLink = $('<a>'), hiddenReloadLinkElem = $(a); hiddenReloadLinkElem.attr('href', link).css('display', 'none !important'); $('body').append(a); hiddenReloadLinkElem.get(0).click(); return false; 

The alternative for pure JS is all over the web.