Javascript:如何真正使用锚标记重新加载网站?

发布于 2024-08-23 01:03:34 字数 332 浏览 7 评论 0原文

以这个代码片段为例:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

寄意 2024-08-30 01:03:34

将指定的 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.

浮华 2024-08-30 01:03:34

使用 URL 的查询部分添加随机字符串:

// Use a timestamp to make sure the page is always reloaded
window.location.href = 'mysite.htm?r='+(+new Date())+'#images';

use the query part of the URL to add a random string:

// Use a timestamp to make sure the page is always reloaded
window.location.href = 'mysite.htm?r='+(+new Date())+'#images';
花开半夏魅人心 2024-08-30 01:03:34

虽然我觉得 alemjerus 的答案是好的方向,但当新的 URL 指向不同的页面时,它不起作用,至少在我的 Chrome v40 上是这样。

例如,当新的 URL 只是添加一个锚点时,它工作正常:

var redirectToURL = '/questions/2319004/javascript-how-to-truly-reload-a-site-with-an-anchor-tag#answer-28621360';
window.location.href = redirectToURL;
window.location.reload(true);

但是当它指向不同的页面时,用户在重新加载后仍将停留在当前页面。 (批量运行以下代码,location.href=location.reload之间没有延迟):

var redirectToURL = 'http://stackoverflow.com';
window.location.href = redirectToURL;
window.location.reload(true);

此时去掉location.reload代码> 作品。所以我的解决方法是检查新的 URL 是否在同一页面上:

var urlParser = document.createElement('a');
urlParser.href = redirectToURL;
if (urlParser.origin + urlParser.pathname === location.origin + location.pathname) {
    window.location.reload(true);
}

在 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:

var redirectToURL = '/questions/2319004/javascript-how-to-truly-reload-a-site-with-an-anchor-tag#answer-28621360';
window.location.href = redirectToURL;
window.location.reload(true);

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= and location.reload):

var redirectToURL = 'http://stackoverflow.com';
window.location.href = redirectToURL;
window.location.reload(true);

In this case, remove the location.reload works. So my workaround is to check whether new URL is on the same page:

var urlParser = document.createElement('a');
urlParser.href = redirectToURL;
if (urlParser.origin + urlParser.pathname === location.origin + location.pathname) {
    window.location.reload(true);
}

Tested on Chrome and Safari, haven't tried IE. Kindly let me know if it doesn't work on any major browser.

少年亿悲伤 2024-08-30 01:03:34

您可能是一个骗子,并附加一个随机查询字符串,重定向到“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.

离不开的别离 2024-08-30 01:03:34

我发现@alemjerus的答案会重新触发POST,如果这是最后一个请求:

window.location.reload(true)

我想要的是使用GET从头开始刷新页面,所以我使用此解决方案

<a href="javascript:window.location.href=window.location.href">Refresh page</a>

I found that the answer by @alemjerus would retrigger a POST if that was the last request:

window.location.reload(true)

What I wanted was to refresh the page from scratch using a GET, so I used this solution:

<a href="javascript:window.location.href=window.location.href">Refresh page</a>
谜泪 2024-08-30 01:03:34

试试这个:
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.

神魇的王 2024-08-30 01:03:34

适用于大多数 Mozilla 浏览器

<a title="Reset this Document" href="javascript:void()" onclick="window.location.href='#'"><font color="red">Reset</font></a>

代码行:

window.location.href='#'
or window.location.href=''

最后的代码在 IE 中无法正常工作

Works with most of the Mozilla browsers

<a title="Reset this Document" href="javascript:void()" onclick="window.location.href='#'"><font color="red">Reset</font></a>

Code Line:

window.location.href='#'
or window.location.href=''

The last code will not work properly with IE

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文