如何使用 JavaScript 刷新页面?
如何使用 JavaScript 刷新页面?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何使用 JavaScript 刷新页面?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(20)
使用
location.reload()
。例如,每当单击带有
id="something"
的元素时重新加载:reload()
函数采用一个可选参数,该参数可以设置为true< /code> 强制从服务器而不是缓存重新加载。该参数默认为
false
,因此默认情况下页面可能会从浏览器的缓存中重新加载。Use
location.reload()
.For example, to reload whenever an element with
id="something"
is clicked:The
reload()
function takes an optional parameter that can be set totrue
to force a reload from the server rather than the cache. The parameter defaults tofalse
, so by default the page may reload from the browser's cache.使用 JavaScript 有
多种无限种方式刷新页面:location.reload()
history.go(0)
location.href = location.href
location.href = location.pathname
location.replace(location.pathname)
location.reload(false)< /代码>
<块引用>
如果我们需要从
再次访问网络服务器(例如文档内容
动态更改)我们会将参数作为
true
传递。您可以继续发挥创意:
window.location = window.location
window.self.window.self.window.window.location = window.location
There are
multipleunlimited ways to refresh a page with JavaScript:location.reload()
history.go(0)
location.href = location.href
location.href = location.pathname
location.replace(location.pathname)
location.reload(false)
You can continue the list being creative:
window.location = window.location
window.self.window.self.window.window.location = window.location
这适用于所有浏览器:
This works on all browsers:
我想很多方法都可以:
window.location.reload();
history.go(0);
window.location.href=window.location .href;
Lots of ways will work, I suppose:
window.location.reload();
history.go(0);
window.location.href=window.location.href;
如果您使用 jQuery 并且想要异步刷新页面的内容,那么您可以执行以下操作:
我在这里使用的方法是 Ajax jQuery。我在 Chrome 13 上测试了它。然后我将代码放入将触发重新加载的处理程序中。 URL 为
""
,这意味着 此页面。请注意,这可能不是提问者所说的“刷新”的意思。 通常,在常见用法中,该术语是指以用户单击浏览器用户界面中提供的“刷新/重新加载”按钮时浏览器执行的方式重新加载页面。然而,这并不是“刷新”的唯一可能的含义,并且在特定情况下它可能是优选的。
此代码的作用是将新的和更新的内容获取到页面。它通过对新内容发出异步 HTTP 请求,然后用新的异步加载内容替换页面的当前内容来实现此目的。它的结果是刷新页面的内容,尽管它的工作方式略有不同。
您必须选择您真正想要的,以及最适合您的特定用例的。
编辑:问题已更改为“使用 javascript”,因此考虑到问题当前的确切措辞,此答案可能不是最准确的。
If you're using jQuery and you want to refresh the contents of a page asynchronously, then you can do the following:
The approach here that I used was Ajax jQuery. I tested it on Chrome 13. Then I put the code in the handler that will trigger the reload. The URL is
""
, which means this page.Note that this may not be what the asker meant by "refresh". Generally, in common usage, that term refers to reloading the page in the way that the browser would do if the user clicked the "Refresh/reload" button provided in the browser's user interface. However, this is not the only possible meaning of "refresh", and it may be preferred in a specific circumstance.
What this code does is get new and updated content to the page. It does this by making an asynchronous HTTP request for the new content, and then replacing the page's current content with the new asynchronously-loaded content. It has the result of refreshing the contents of the page, even though it works slightly differently.
You'll have to choose which one you actually want, and which works best for your specific use-case.
EDIT: The question has been changed to "using javascript" so this answer might not be the most accurate considering the current exact wording of the question.
如果当前页面是通过 POST 请求加载的,您可能需要使用 ,
因为
如果在通过 POST 请求加载的页面上调用
window.location.reload()
将会提示确认。If the current page was loaded by a POST request, you may want to use
instead of
because
window.location.reload()
will prompt for confirmation if called on a page that was loaded by a POST request.你的选择太多了;以下任何工作:
window.location.href = window.location.href;
window.location.reload();
history.go(0);
You're spoiled for choice; any of the following work:
window.location.href = window.location.href;
window.location.reload();
history.go(0);
您可能想要使用
forceGet
,它是一个布尔值且可选。默认值为 false,从缓存中重新加载页面。
如果您想强制浏览器从服务器获取页面以摆脱缓存,请将此参数设置为 true。
或者只是
如果您想要快速轻松地进行缓存。
You may want to use
forceGet
is a boolean and optional.The default is false which reloads the page from the cache.
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.
Or just
if you want quick and easy with caching.
具有不同缓存相关行为的三种方法:
location.reload(true)
在实现了
location.reload()
的forcedReload
参数的浏览器中,通过获取页面及其所有资源(脚本、样式表、图像等)。不会从缓存中提供任何资源 - 从服务器获取新副本,而不发送任何if-modified-since
或if-none-match
请求中的标头。相当于用户在可能的情况下在浏览器中进行“硬重新加载”。
请注意,Firefox 支持将
true
传递给location.reload()
(请参阅 MDN)和 Internet Explorer(请参阅 MSDN),但并未得到普遍支持,并且不是 W3 HTML 5 规范,也不是 W3 草案 HTML 5.1 规范,也不是 WHATWG HTML 生活标准。在不支持的浏览器(例如 Google Chrome)中,
location.reload(true)
的行为与location.reload()
相同。location.reload()
或location.reload(false)
< /strong>重新加载页面,获取页面 HTML 本身的新的、未缓存的副本,并执行 RFC 7234 对浏览器已缓存的任何资源(如脚本)的重新验证请求,即使它们是 新鲜是 RFC 7234 允许浏览器无需重新验证即可提供服务。
据我所知,浏览器在执行
location.reload()
调用时应如何利用其缓存的确切方式尚未指定或记录;我通过实验确定了上述行为。这相当于用户只需按浏览器中的“刷新”按钮。
location = location
(或无数其他可能的技术,涉及分配给location
或其属性)仅当页面的 URL 不包含 fragid/hashbang 时才有效!
重新加载页面,而不重新获取或重新验证任何 fresh 从缓存中获取资源。如果页面的 HTML 本身是新鲜的,这将重新加载页面而不执行任何 HTTP 请求。
这相当于(从缓存的角度来看)用户在新选项卡中打开页面。
但是,如果页面的 URL 包含哈希值,则这将不起作用。
同样,据我所知,这里的缓存行为是未指定的;我是通过测试确定的。
因此,总而言之,您希望使用:
location = location
来最大限度地利用缓存,只要页面的 URL 中没有哈希值,在这种情况下,location.reload(true)
无法location.reload()< /code> 在某些浏览器中,例如 Chrome)
location.reload()
来忠实地再现用户单击“刷新”按钮的效果。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 any resources 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 7234 revalidation requests for any resources (like scripts) that the browser has cached, even if they are fresh are 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 any fresh resources 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.
So, in summary, you want to use:
location = location
for maximum use of the cache, as long as the 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.window.location.reload()
将从服务器重新加载,并再次加载所有数据、脚本、图像等。因此,如果您只想刷新 HTML,
window.location = document.URL
将返回得更快且流量更少。但如果 URL 中包含井号 (#),则不会重新加载页面。window.location.reload()
will reload from the server and will load all your data, scripts, images, etc. again.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.jQuery
Load
函数还可以执行页面刷新:The jQuery
Load
function can also perform a page refresh:由于问题是通用的,让我们尝试总结一下可能的解决方案:
简单的纯 JavaScript 解决方案:
最简单的方法是以适当的方式放置单行解决方案:
许多人在这里缺少什么,因为他们希望得到一些“点”是 reload() 函数本身提供了一个布尔值作为参数(详细信息:https://developer.mozilla.org/en-US/docs/Web/API/Location/reload)。
这意味着有两种方法:
解决方案 1:强制从服务器重新加载当前页面
解决方案 2:从缓存或服务器重新加载(基于浏览器和您的配置)
如果您愿意要将其与 jQuery 结合起来监听事件,我建议使用“.on()”方法而不是“.click”或其他事件包装器,例如更合适的解决方案是:
As the question is generic, let's try to sum up possible solutions for the answer:
Simple plain JavaScript Solution:
The easiest way is a one line solution placed in an appropriate way:
What many people are missing here, because they hope to get some "points" is that the reload() function itself offers a Boolean as a parameter (details: https://developer.mozilla.org/en-US/docs/Web/API/Location/reload).
This means there are two ways:
Solution1: Force reloading the current page from the server
Solution2: Reloading from cache or server (based on browser and your config)
And if you want to combine it with jQuery an listening to an event, I would recommend using the ".on()" method instead of ".click" or other event wrappers, e.g. a more proper solution would be:
这是一个使用 jQuery 异步重新加载页面的解决方案。它避免了由
window.location = window.location
引起的闪烁。此示例显示了一个不断重新加载的页面,如仪表板中一样。它经过了实战考验,正在时代广场的信息显示电视上运行。注意:
$.ajax
就像$.get('',function(data){$(document.body).html(data)})
导致 css/js 文件被缓存破坏,即使您使用cache: true
,这就是我们使用parseHTML
parseHTML
不会找到body
标签,所以你的整个 body 需要放在一个额外的 div 中,我希望这些知识点有一天对你有帮助,你可以猜出我们是如何为该div
选择 id 的,http-equiv="refresh"
以防万一 javascript/服务器出现问题,那么页面仍然会重新加载,而无需接到电话http-equiv
刷新修复了这个问题Here is a solution that asynchronously reloads a page using jQuery. It avoids the flicker caused by
window.location = window.location
. This example shows a page that reloads continuously, as in a dashboard. It is battle-tested and is running on an information display TV in Times Square.Notes:
$.ajax
directly like$.get('',function(data){$(document.body).html(data)})
causes css/js files to get cache-busted, even if you usecache: true
, that's why we useparseHTML
parseHTML
will NOT find abody
tag so your whole body needs to go in an extra div, I hope this nugget of knowledge helps you one day, you can guess how we chose the id for thatdiv
http-equiv="refresh"
just in case something goes wrong with javascript/server hiccup, then the page will STILL reload without you getting a phone callhttp-equiv
refresh fixes that我发现
或者
还刷新了页面。
这使得重新加载页面并删除任何哈希值变得非常容易。
当我在 iOS 模拟器中使用 AngularJS 时,这非常好,这样我就不必重新运行应用程序。
I found
or
also makes a page refresh.
This makes it very much easier to reload the page removing any hash.
This is very nice when I am using AngularJS in the iOS simulator, so that I don't have to rerun the app.
您可以使用 JavaScript
location.reload()
方法。该方法接受一个布尔参数。
true
或false
。如果参数为true
;该页面总是从服务器重新加载。如果为假
;这是默认值或使用空参数浏览器从缓存中重新加载页面。使用
true
参数使用
default
/false
参数使用 jquery
You can use JavaScript
location.reload()
method.This method accepts a boolean parameter.
true
orfalse
. If the parameter istrue
; the page always reloaded from the server. If it isfalse
; which is the default or with empty parameter browser reload the page from it's cache.With
true
parameterWith
default
/false
parameterUsing jquery
如果您正在使用 jQuery 并且想要刷新,请尝试在 javascript 函数中添加 jQuery:
我想在单击 oh an
h3
时从页面隐藏 iframe,对我来说它有效,但我没有'除非我手动刷新浏览器,否则无法单击允许我查看iframe
的项目...这并不理想。我尝试了以下方法:
将普通的 Jane javascript 与您的 jQuery 混合应该可以工作。
If you are using jQuery and want to refresh, then try adding your jQuery in a javascript function:
I wanted to hide an iframe from a page when clicking oh an
h3
, for me it worked but I wasn't able to click the item that allowed me to view theiframe
to begin with unless I refreshed the browser manually...not ideal.I tried the following:
Mixing plain Jane javascript with your jQuery should work.
您可以用两种方式编写它。第一种是重新加载页面的标准方法,也称为简单刷新
,另一种称为硬刷新。在这里,您传递布尔表达式并将其设置为 true。这将重新加载页面,破坏旧的缓存并从头开始显示内容。
You can write it in two ways. 1st is the standard way of reloading the page also called as simple refresh
And another is called the hard refresh. Here you pass the boolean expression and set it to true. This will reload the page destroying the older cache and displaying the contents from scratch.
可能最短(12 个字符) - 使用 历史记录
Probably shortest (12 chars) - use history
它是 JavaScript 中最短的。
It is shortest in JavaScript.
您可以简单地使用
location.reload()
。You can simply use
location.reload()
.