简体   繁体   English

哈希链接重新加载页面

[英]hash link reloads page

I have a code snippet that is installed on third party websites.我有一个安装在第三方网站上的代码片段。 I can't get into the details, but it loads HTML, CSS and JS onto the page through the use of a <script> tag.我无法深入了解细节,但它通过使用<script>标签将 HTML、CSS 和 JS 加载到页面上。

Part of the code is a JS function that executes when this link is clicked:部分代码是一个 JS 函数,当点击这个链接时会执行:

<a href="#">?</a>

If there are JS errors on the page that prevent the function from executing, clicking the link obviously just adds the hash to the URL and takes the user to the top of the page.如果页面上有阻止函数执行的 JS 错误,单击链接显然只是将哈希添加到 URL 并将用户带到页面顶部。 This is the expected fallback behavior.这是预期的回退行为。

However, on one third party site with a multitude of JS errors, clicking the link removes everything after the top level domain, adds the hash, and directs to that page (the home page).但是,在一个存在大量 JS 错误的第三方站点上,单击该链接会删除顶级域之后的所有内容、添加哈希值并定向到该页面(主页)。 For example, the link would take the user from:例如,该链接会将用户从:

http://www.example.com/2010/05/14/very-interesting-blog-post/

to

http://www.example.com/#

Notably, the issue occurs in Firefox and Chrome, but not IE9.值得注意的是,该问题发生在 Firefox 和 Chrome 中,而不是 IE9。 I know it may be impossible to properly diagnose the issue without more detailed code, but I'm not at liberty to provide it.我知道没有更详细的代码可能无法正确诊断问题,但我不能随意提供它。 I'm just hoping to get some kind of reasonable explanation for this strange browser behavior.我只是希望对这种奇怪的浏览器行为得到某种合理的解释。

If it helps at all, the site in question is a WordPress blog.如果它有帮助,那么有问题的网站是一个 WordPress 博客。 Thanks in advance.提前致谢。

EDIT: This is apparently not caused by any JS on the site, because turning off JS and adding the link with the inspector produced the same behavior.编辑:这显然不是由网站上的任何 JS 引起的,因为关闭 JS 并添加与检查器的链接会产生相同的行为。

I had a similar error where clicking on any <a href="#"> causes a full page reload.我有一个类似的错误,点击任何<a href="#">会导致整个页面重新加载。 I managed to solve this by removing the <base href="/"> tag from the <head> of the page.我设法通过从页面的<head>中删除<base href="/">标签来解决这个问题。 I couldn't find any informations on this (yet).我找不到任何有关此的信息(还)。 I'll add more if I can find any additional info.如果我能找到任何其他信息,我会添加更多。

This problem happens when you use windows.onpopstate to handle back or forward buttons and load page using ajax.当您使用 windows.onpopstate 处理后退或前进按钮并使用 ajax 加载页面时,会发生此问题。 Try to solve your js code for handling history.尝试解决处理历史记录的 js 代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM