Javascript:如何真正使用锚标记重新加载网站?
以这个代码片段为例:
window.location.href = 'mysite.htm#images';
已经在网站 mysite.htm
上并通过 location.href
触发重新加载不会重新加载页面,而是跳转到其中指定的锚点网址。
我需要一种机制来真正重新加载页面。有什么想法吗?
PS:我尝试了位置的其他方法,但没有一个能解决问题:(
编辑
不要拘泥于文件类型 htm
。我也需要它来创建动态网站。
Take this code snippet for example:
window.location.href = 'mysite.htm#images';
Already being on the site mysite.htm
and triggering the reload thru location.href
won't reload the page but jump to the anchor named within the URL.
I need a mechanism to truly reload the page. Any ideas?
PS: I tried location's other methods but none of them does the trick :(
EDIT
Don't hang on the filetype htm
. I'd need it for dynamic sites as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
将指定的 URL 设置为 location 后,发出
window.location.reload(true)
。这将强制浏览器重新加载页面。After you set the specified URL into location, issue
window.location.reload(true)
. This will force browser to reload the page.使用 URL 的查询部分添加随机字符串:
use the query part of the URL to add a random string:
虽然我觉得 alemjerus 的答案是好的方向,但当新的 URL 指向不同的页面时,它不起作用,至少在我的 Chrome v40 上是这样。
例如,当新的 URL 只是添加一个锚点时,它工作正常:
但是当它指向不同的页面时,用户在重新加载后仍将停留在当前页面。 (批量运行以下代码,
location.href=
和location.reload
之间没有延迟):此时去掉
location.reload
代码> 作品。所以我的解决方法是检查新的 URL 是否在同一页面上:在 Chrome 和 Safari 上测试,还没有尝试过 IE。如果它不适用于任何主要浏览器,请告诉我。
While I feel alemjerus's answer is on the good direction, it doesn't work when the new URL points to different page, at least on my Chrome v40.
Example, when the new URL just add an anchor, it works fine:
But when it points to a different page, user will still stay on the current page after reload. (Run the following codes in a batch, no delay between
location.href=
andlocation.reload
):In this case, remove the
location.reload
works. So my workaround is to check whether new URL is on the same page:Tested on Chrome and Safari, haven't tried IE. Kindly let me know if it doesn't work on any major browser.
您可能是一个骗子,并附加一个随机查询字符串,重定向到“mysite.htm?random=289954034#images”。
但如果这是一个静态页面,正如
.htm
所暗示的那样,您需要重新加载做什么?如果您的 onload Javascript 可以第二次发生,也许您只需要更改应用程序的流程即可。You could be a cheater and append a randomized query string, redirecting to "mysite.htm?random=289954034#images".
But if this is a static page, as
.htm
implies, what do you need reloading for? If it's so that your onload Javascript can occur a second time, maybe you just need to change your application's flow.我发现@alemjerus的答案会重新触发
POST
,如果这是最后一个请求:我想要的是使用
GET
从头开始刷新页面,所以我使用此解决方案:I found that the answer by @alemjerus would retrigger a
POST
if that was the last request:What I wanted was to refresh the page from scratch using a
GET
, so I used this solution:试试这个:
window.location.href = 'mysite.htm?';
编辑:这仅在第一次有效;之后,缓存再次生效。
Try this:
window.location.href = 'mysite.htm?';
EDIT: This will only work the first time; after that, the caching takes effect again.
适用于大多数 Mozilla 浏览器
代码行:
最后的代码在 IE 中无法正常工作
Works with most of the Mozilla browsers
Code Line:
The last code will not work properly with IE