使用 JavaScript 或 HTML 刷新页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5294842/
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
Refresh a page using JavaScript or HTML
提问by Prabakaran
How can I refresh a page using JavaScript or HTML?
如何使用 JavaScript 或 HTML 刷新页面?
回答by Reid
window.location.reload();
in JavaScript
window.location.reload();
在 JavaScript 中
<meta http-equiv="refresh" content="1">
in HTML (where 1
= 1 second).
<meta http-equiv="refresh" content="1">
在 HTML 中(其中1
= 1 秒)。
回答by epoch
Here are 535 ways to reloada page using javascript, very cool:
这里有535 种使用 javascript重新加载页面的方法,非常酷:
Here are the first 20:
这是前20个:
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)
and the last 10:
和最后 10 个:
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']()
回答by Wolv3rine
simply use..
简单地使用..
location.reload(true/false);
If false, the page will be reloaded from cache, else from the server.
如果为 false,则页面将从缓存重新加载,否则从服务器重新加载。
回答by Mike Lewis
window.location.reload()
should work however there are many different options like:
应该可以工作,但是有许多不同的选项,例如:
window.location.href=window.location.href
回答by Mandeep Singh
You can also use
你也可以使用
<input type="button" value = "Refresh" onclick="history.go(0)" />
It works fine for me.
这对我来说可以。
回答by Rob
Use:
用:
window.location.reload();
回答by Codebeat
If it has something to do control updates on cached pages here I have a nice method how to do this.
如果它可以控制缓存页面上的更新,我有一个很好的方法来做到这一点。
- add some javascript to the page that always get the versionnumber of the page as a string (ajax) at loading. for example:
www.yoursite.com/page/about?getVer=1&__[date]
- Compare it to the stored versionnumber (stored in cookie or localStorage) if user has visited the page once, otherwise store it directly.
- If version is not the same as local version, refresh the page using window.location.reload(true)
- You will see any changes made on the page.
- 向页面添加一些 javascript,在加载时总是以字符串(ajax)的形式获取页面的版本号。例如:
www.yoursite.com/page/about?getVer=1&__[date]
- 如果用户访问过该页面,则将其与存储的版本号(存储在 cookie 或 localStorage 中)进行比较,否则直接存储。
- 如果版本与本地版本不同,请使用 window.location.reload(true) 刷新页面
- 您将看到页面上所做的任何更改。
This method requires at least one request even when no request is needed because it already exists in the local browser cache. But the overhead is less comparing to using no cache at all (to be sure the page will show the right updated content). This requires just a few bytes for each page request instead of all content for each page.
即使不需要请求,此方法也至少需要一个请求,因为它已存在于本地浏览器缓存中。但是与完全不使用缓存相比,开销要小一些(以确保页面将显示正确的更新内容)。每个页面请求只需要几个字节,而不是每个页面的所有内容。
IMPORTANT: The version info request must be implemented on your server otherwise it will return the whole page.
重要提示:版本信息请求必须在您的服务器上实现,否则它将返回整个页面。
Example of version string returned by www.yoursite.com/page/about?getVer=1&__[date]
:
skg2pl-v8kqb
返回的版本字符串示例www.yoursite.com/page/about?getVer=1&__[date]
:skg2pl-v8kqb
To give you an example in code, here is a part of my library (I don't think you can use it but maybe it gives you some idea how to do it):
给你一个代码示例,这是我库的一部分(我认为你不能使用它,但也许它可以让你知道如何去做):
o.gCheckDocVersion = function() // Because of the hard caching method, check document for changes with ajax
{
var sUrl = o.getQuerylessUrl(window.location.href),
sDocVer = o.gGetData( sUrl, false );
o.ajaxRequest({ url:sUrl+'?getVer=1&'+o.uniqueId(), cache:0, dataType:'text' },
function(sVer)
{
if( typeof sVer == 'string' && sVer.length )
{
var bReload = (( typeof sDocVer == 'string' ) && sDocVer != sVer );
if( bReload || !sDocVer )
{
o.gSetData( sUrl, sVer );
sDocVer = o.gGetData( sUrl, false );
if( bReload && ( typeof sDocVer != 'string' || sDocVer != sVer ))
{ bReload = false; }
}
if( bReload )
{ // Hard refresh page contents
window.location.reload(true); }
}
}, false, false );
};
If you are using version independent resources like javascript or css files, add versionnumbers (implemented with a url rewrite and not with a query because they mostly won't be cached). For example: www.yoursite.com/ver-01/about.js
如果您使用的是 javascript 或 css 文件等与版本无关的资源,请添加版本号(使用 url 重写而不是查询来实现,因为它们大多不会被缓存)。例如:www.yoursite.com/ver-01/about.js
For me, this method is working great, maybe it can help you too.
对我来说,这个方法效果很好,也许它也可以帮助你。
回答by Muhammad Waqas
try this working fine
试试这个工作正常
jQuery("body").load(window.location.href);