Javascript:如何真正重新加载带有锚标记的站点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2319004/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:45:17  来源:igfitidea点击:

Javascript: How to truly reload a site with an anchor tag?

javascripturlanchorreload

提问by aefxx

Take this code snippet for example:

以这个代码片段为例:

window.location.href = 'mysite.htm#images';

Already being on the site mysite.htmand triggering the reload thru location.hrefwon't reload the page but jump to the anchor named within the URL.

已经在站点上mysite.htm并通过触发重新加载location.href不会重新加载页面,而是跳转到 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 :(

PS:我尝试了 location 的其他方法,但没有一个能奏效:(

EDIT
Don't hang on the filetype htm. I'd need it for dynamic sites as well.

编辑
不要挂在文件类型上htm。我也需要它用于动态网站。

回答by alemjerus

After you set the specified URL into location, issue window.location.reload(true). This will force browser to reload the page.

将指定的 URL 设置为 location 后,发出window.location.reload(true). 这将强制浏览器重新加载页面。

回答by Andy E

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

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

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

回答by ZZY

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.

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

Example, when the new URL just add an anchor, it works fine:

例如,当新 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);

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

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

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

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

在这种情况下,请移除location.reload作品。所以我的解决方法是检查新 URL 是否在同一页面上:

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.

在 Chrome 和 Safari 上测试过,没有尝试过 IE。如果它在任何主要浏览器上都不起作用,请告诉我。

回答by Matchu

You could be a cheater and append a randomized query string, redirecting to "mysite.htm?random=289954034#images".

您可能是个骗子并附加一个随机查询字符串,重定向到“mysite.htm?random=289954034#images”。

But if this is a static page, as .htmimplies, 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.

但是,如果这是一个静态页面,正如.htm暗示的那样,您需要重新加载什么?如果您的 onload Javascript 可以再次发生,那么您可能只需要更改应用程序的流程。

回答by Cameron

Try this: window.location.href = 'mysite.htm?';

尝试这个: window.location.href = 'mysite.htm?';

EDIT:This will only work the first time; after that, the caching takes effect again.

编辑:这只会在第一次工作;之后,缓存再次生效。

回答by Ramon Maldio

Works with most of the Mozilla browsers

适用于大多数 Mozilla 浏览器

<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

最后的代码在 IE 下不能正常工作