Javascript 如何使用 jQuery 刷新页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5404839/
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
How can I refresh a page with jQuery?
提问by luca
How can I refresh a page with jQuery?
如何使用 jQuery 刷新页面?
回答by Roy
Use location.reload()
:
$('#something').click(function() {
location.reload();
});
The reload()
function takes an optional parameter that can be set to true
to force a reload from the server rather than the cache. The parameter defaults to false
, so by default the page may reload from the browser's cache.
该reload()
函数采用一个可选参数,该参数可以设置true
为强制从服务器而不是缓存重新加载。该参数默认为false
,因此默认情况下页面可能会从浏览器的缓存中重新加载。
回答by Thorben
This should work on all browsers even without jQuery:
即使没有 jQuery,这也应该适用于所有浏览器:
location.reload();
回答by Ionic? Biz?u
There are multipleunlimited ways to refresh a page with JavaScript:
使用 JavaScript 刷新页面有多种无限方式:
location.reload()
history.go(0)
location.href = location.href
location.href = location.pathname
location.replace(location.pathname)
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()
history.go(0)
location.href = location.href
location.href = location.pathname
location.replace(location.pathname)
location.reload(false)
如果我们需要再次从网络服务器中提取文档(例如文档内容动态更改的地方),我们会将参数作为
true
.
You can continue the list being creative:
您可以继续使列表富有创意:
window.location = window.location
window.self.window.self.window.window.location = window.location
- ...and other 534 ways
window.location = window.location
window.self.window.self.window.window.location = window.location
- ...以及其他 534 种方式
var methods = [
"location.reload()",
"history.go(0)",
"location.href = location.href",
"location.href = location.pathname",
"location.replace(location.pathname)",
"location.reload(false)"
];
var $body = $("body");
for (var i = 0; i < methods.length; ++i) {
(function(cMethod) {
$body.append($("<button>", {
text: cMethod
}).on("click", function() {
eval(cMethod); // don't blame me for using eval
}));
})(methods[i]);
}
button {
background: #2ecc71;
border: 0;
color: white;
font-weight: bold;
font-family: "Monaco", monospace;
padding: 10px;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.5s ease;
margin: 2px;
}
button:hover {
background: #27ae60;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
回答by David
Lots of ways will work, I suppose:
很多方法都会起作用,我想:
window.location.reload();
history.go(0);
window.location.href=window.location.href;
window.location.reload();
history.go(0);
window.location.href=window.location.href;
回答by Peter
To reload a page with jQuery, do:
要使用 jQuery 重新加载页面,请执行以下操作:
$.ajax({
url: "",
context: document.body,
success: function(s,x){
$(this).html(s);
}
});
The approach here that I used was Ajax jQuery. I tested it on Chrome13. Then I put the code in the handler that will trigger the reload. The URLis ""
, which means this page.
我在这里使用的方法是 Ajax jQuery。我在Chrome13上对其进行了测试。然后我将代码放入将触发重新加载的处理程序中。该URL是""
,这意味着这个页面。
回答by SumairIrshad
If the current page was loaded by a POST request, you may want to use
如果当前页面是由 POST 请求加载的,您可能需要使用
window.location = window.location.pathname;
instead of
代替
window.location.reload();
because window.location.reload()
will prompt for confirmation if called on a page that was loaded by a POST request.
因为window.location.reload()
如果在由 POST 请求加载的页面上调用,则会提示确认。
回答by JohnP
The question should be,
问题应该是,
How to refresh a page with JavaScript
如何使用JavaScript刷新页面
window.location.href = window.location.href; //This is a possibility
window.location.reload(); //Another possiblity
history.go(0); //And another
You're spoiled for choice.
你被宠坏了。
回答by Pinch
You may want to use
您可能想要使用
location.reload(forceGet)
forceGet
is a boolean and optional.
forceGet
是一个布尔值和可选的。
The default is false which reloads the page from the cache.
默认值为 false,从缓存中重新加载页面。
Set this parameter to true if you want to force the browser to get the page from the server to get rid of the cache as well.
如果您想强制浏览器从服务器获取页面以摆脱缓存,请将此参数设置为 true。
Or just
要不就
location.reload()
if you want quick and easy with caching.
如果您想快速轻松地进行缓存。
回答by Mark Amery
Three approaches with different cache-related behaviours:
具有不同缓存相关行为的三种方法:
location.reload(true)
In browsers that implement the
forcedReload
parameter oflocation.reload()
, reloads by fetching a fresh copy of the page and all of its resources (scripts, stylesheets, images, etc.). Will not serve anyresources from the cache - gets fresh copies from the server without sending anyif-modified-since
orif-none-match
headers in the request.Equivalent to the user doing a "hard reload" in browsers where that's possible.
Note that passing
true
tolocation.reload()
is supported in Firefox (see MDN) and Internet Explorer (see MSDN) but is not supported universally and is not part of the W3 HTML 5 spec, nor the W3 draft HTML 5.1 spec, nor the WHATWG HTML Living Standard.In unsupporting browsers, like Google Chrome,
location.reload(true)
behaves the same aslocation.reload()
.location.reload()
orlocation.reload(false)
Reloads the page, fetching a fresh, non-cached copy of the page HTML itself, and performing RFC 7234revalidation requests for any resources (like scripts) that the browser has cached, even if they are freshare RFC 7234 permits the browser to serve them without revalidation.
Exactly how the browser should utilise its cache when performing a
location.reload()
call isn't specified or documented as far as I can tell; I determined the behaviour above by experimentation.This is equivalent to the user simply pressing the "refresh" button in their browser.
location = location
(or infinitely many other possible techniques that involve assigning tolocation
or to its properties)Only works if the page's URL doesn't contain a fragid/hashbang!
Reloads the page without refetching or revalidating anyfreshresources from the cache. If the page's HTML itself is fresh, this will reload the page without performing any HTTP requests at all.
This is equivalent (from a caching perspective) to the user opening the page in a new tab.
However, if the page's URL contains a hash, this will have no effect.
Again, the caching behaviour here is unspecified as far as I know; I determined it by testing.
location.reload(true)
在实现
forcedReload
参数的浏览器中location.reload()
,通过获取页面及其所有资源(脚本、样式表、图像等)的新副本来重新加载。不会从缓存中提供任何资源 - 从服务器获取新副本,而无需在请求中发送任何if-modified-since
或if-none-match
标头。相当于用户在可能的情况下在浏览器中执行“硬重新加载”。
请注意,Firefox(请参阅MDN)和 Internet Explorer(请参阅MSDN)支持传递
true
到,但并非普遍支持,并且不是W3 HTML 5 规范、W3 草案 HTML 5.1 规范和WHATWG HTML Living Standard 的一部分。location.reload()
在不支持的浏览器(如 Google Chrome)中,
location.reload(true)
行为与location.reload()
.location.reload()
或者location.reload(false)
重新加载页面,获取页面 HTML 本身的新的、非缓存的副本,并对浏览器缓存的任何资源(如脚本)执行RFC 7234重新验证请求,即使它们是新鲜的 RFC 7234 允许浏览器提供服务他们没有重新验证。
location.reload()
据我所知,在执行调用时浏览器应如何利用其缓存的确切方式并未指定或记录在案;我通过实验确定了上述行为。这相当于用户只需按下浏览器中的“刷新”按钮。
location = location
(或无数其他可能的技术,涉及location
为其属性赋值)仅当页面的 URL 不包含 fragid/hashbang 时才有效!
重新加载页面而不从缓存中重新获取或重新验证任何新资源。如果页面的 HTML 本身是新鲜的,这将重新加载页面而不执行任何 HTTP 请求。
这相当于(从缓存的角度)用户在新选项卡中打开页面。
但是,如果页面的 URL 包含哈希值,这将不起作用。
同样,据我所知,这里的缓存行为是未指定的;我通过测试确定了它。
So, in summary, you want to use:
因此,总而言之,您要使用:
location = location
for maximum use of the cache, as long asthe page doesn't have a hash in its URL, in which case this won't worklocation.reload(true)
to fetch new copies of all resources without revalidating (although it's not universally supported and will behave no differently tolocation.reload()
in some browsers, like Chrome)location.reload()
to faithfully reproduce the effect of the user clicking the 'refresh' button.
location = location
为了最大限度地利用缓存,只要页面的 URL 中没有哈希值,在这种情况下这将不起作用location.reload(true)
在不重新验证的情况下获取所有资源的新副本(尽管它并未得到普遍支持,并且location.reload()
在某些浏览器(如 Chrome)中的行为没有不同)location.reload()
忠实再现用户点击“刷新”按钮的效果。
回答by Mark Amery
window.location.reload()
will reload from the server and will load all your data, scripts, images, etc. again.
window.location.reload()
将从服务器重新加载并再次加载您的所有数据、脚本、图像等。
So if you just want to refresh the HTML, the window.location = document.URL
will return much quicker and with less traffic. But it will not reload the page if there is a hash (#) in the URL.
因此,如果您只想刷新 HTML,window.location = document.URL
它将以更少的流量返回得更快。但如果 URL 中有哈希 (#),它不会重新加载页面。