Prevent default hash behavior on page load

时间:2022-10-19 00:09:30

I have a page with a tabbed interface. Each tab has a unique id. I've enabled links pointing to that page with the appended id after the hash, and I'm now trying to circumvent the default browser behavior that opens a URL with hash at the location of the element on the page.

我有一个带有选项卡式界面的页面。每个标签都有一个唯一的ID。我已经启用了指向该页面的链接,并在哈希之后附加了id,我现在正试图绕过默认的浏览器行为,该行为在页面上元素的位置打开带有哈希的URL。

So:

  • pageA links to pageB like this: <a href="pageB.php#Tab4">Go</a>
  • pageA链接到pageB,如下所示:转到

  • pageB opens, and my jQuery activates the correct tab, but the browser has scrolled down to where <div id="Tab4"> is located on the page.
  • pageB打开,我的jQuery激活了正确的选项卡,但浏览器已向下滚动到页面上

    的位置。

That's exactly what I want to prevent.

这正是我想要阻止的。

Any ideas? Thanks!

有任何想法吗?谢谢!

4 个解决方案

#1


44  

There isn't a way to prevent the default hash behavior, but you can alter your hash scheme so the #tag doesn't correspond to any ID on your page. On page load, take the hash value and append a constant (ex: _hash), then scroll to that using jQuery.

没有办法阻止默认的哈希行为,但您可以更改哈希方案,以使#tag与页面上的任何ID不对应。在页面加载时,获取哈希值并附加一个常量(例如:_hash),然后使用jQuery滚动到该值。

Example: http://mysite/page.php#tab4

page.php has <div id="tab4_hash"></div>

page.php有

On page load, get the div by doing tab4 + _hash

在页面加载时,通过执行tab4 + _hash获取div

#2


3  

I would use this:

我会用这个:

window.scrollTo(0,0);

This way you don't need to change element id's or anything else.

这样您就不需要更改元素ID或其他任何内容。

I came across the problem where I wanted to scroll down to the tab, but for some reason it scrolled past it to the bottom of the page (no, there was not a duplicate id). I think it is because the browser would scroll to the id before the content finished loading, and the extra content pushed the element above the new scroll position). I fixed it with this:

我遇到了问题,我想向下滚动到选项卡,但由于某种原因,它滚动到它的页面底部(不,没有重复的ID)。我认为这是因为浏览器会在内容完成加载之前滚动到id,而额外的内容会将元素推到新的滚动位置之上。我修好了这个:

if(document.location.hash)
{
    window.location = document.location.hash;
}

the "if" statement here is mandatory or you might get an infinite loop

这里的“if”语句是强制性的,或者您可能会获得无限循环

#3


1  

After you're done loading the right tab, run this:

完成加载右侧选项卡后,运行以下命令:

window.location = '#';

#4


0  

  1. You should just hide the element with id=hash by default
  2. 你应该默认隐藏id = hash的元素

  3. Аnd then, after the page loads, you make the element visible.
  4. 然后,在页面加载后,您可以使元素可见。

You can find more info here: How to disable anchor "jump" when loading a page?

你可以在这里找到更多信息:如何在加载页面时禁用锚点“跳转”?

#1


44  

There isn't a way to prevent the default hash behavior, but you can alter your hash scheme so the #tag doesn't correspond to any ID on your page. On page load, take the hash value and append a constant (ex: _hash), then scroll to that using jQuery.

没有办法阻止默认的哈希行为,但您可以更改哈希方案,以使#tag与页面上的任何ID不对应。在页面加载时,获取哈希值并附加一个常量(例如:_hash),然后使用jQuery滚动到该值。

Example: http://mysite/page.php#tab4

page.php has <div id="tab4_hash"></div>

page.php有

On page load, get the div by doing tab4 + _hash

在页面加载时,通过执行tab4 + _hash获取div

#2


3  

I would use this:

我会用这个:

window.scrollTo(0,0);

This way you don't need to change element id's or anything else.

这样您就不需要更改元素ID或其他任何内容。

I came across the problem where I wanted to scroll down to the tab, but for some reason it scrolled past it to the bottom of the page (no, there was not a duplicate id). I think it is because the browser would scroll to the id before the content finished loading, and the extra content pushed the element above the new scroll position). I fixed it with this:

我遇到了问题,我想向下滚动到选项卡,但由于某种原因,它滚动到它的页面底部(不,没有重复的ID)。我认为这是因为浏览器会在内容完成加载之前滚动到id,而额外的内容会将元素推到新的滚动位置之上。我修好了这个:

if(document.location.hash)
{
    window.location = document.location.hash;
}

the "if" statement here is mandatory or you might get an infinite loop

这里的“if”语句是强制性的,或者您可能会获得无限循环

#3


1  

After you're done loading the right tab, run this:

完成加载右侧选项卡后,运行以下命令:

window.location = '#';

#4


0  

  1. You should just hide the element with id=hash by default
  2. 你应该默认隐藏id = hash的元素

  3. Аnd then, after the page loads, you make the element visible.
  4. 然后,在页面加载后,您可以使元素可见。

You can find more info here: How to disable anchor "jump" when loading a page?

你可以在这里找到更多信息:如何在加载页面时禁用锚点“跳转”?