Scrolling a page using location.hash in Safari

安稳与你 提交于 2019-12-05 12:37:14

You could reset it first:

window.location.hash = "";
window.location.hash = "page_top";

I needed to add:

<div><a name="page_top"></a></div>
<div><a name="page_topnot"></a></div>

Using "a name" instead of "a href". Works great!

Answering my own question. Gumbo was on the right track, but not quite there.

Safari doesn't like location.hash being set to a blank value. Instead you need to set it to a real anchor value.

So along at the top of the page I now have:

<div><a href="page_top"></a></div>
<div><a href="page_topnot"></a></div>

I found that I needed the divs around the anchors otherwise Safari was scrolling to an unpredictable part of the page rather than to the anchors.

Then to scroll to the top of the page I have to do:

window.location.hash = "page_topnot";
window.location.hash = "page_top";

With that in place Safari will scroll to top of the page every time.

I had the same problem as you had and this solution worked - the only thing I noticed was that in IE, I could hear two clicks - So I did a variation of yours and just put one anchor and it worked as well (ie, call twice, the first time, a non-existent anchor, the second time the real one)

On top, my anchor:

(tag a)name="top" id="top"(end of tag a)

Within javascript, the two calls:

window.location.hash ="topnot";

window.location.hash ="top";

Thanks for your help!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!