Safari ignoring/removing hashatags when clicking h

2019-01-24 02:23发布

问题:

I need to point one page to another and then scroll to a particular section. In Chrome doing www.example.com#section1 would do the trick (#section1 can be an anchor or an element's id).

I have tried it in Chrome and Firefox and in both browsers it works fine (takes me to the page and then scrolls automatically to the anchor however in safari the hash disappears when I click the link.

Why is this happening? Is it possible to do it on Safari, if not how can I go around this problem?

回答1:

When using hyperlinks that point to inside sections on other pages you must remember to add a slash (/) before the hashtag for cross browser compatibility.

Eg: www.example.com/#item-1

doing www.example.com#item-1 isn't accepted by all browsers (apparently by Chrome and Firefox it is)



回答2:

Case: I have parameters after '#' like url#myParam=123, when I changed params like url#myParam=789 Safari sometimes loaded previous page based on myParam=123,

although in Address bar it showed myParam=789

Solution : Use url?#myParam=123, then Safari will everytime load new page.
Using '?' before '#' solved my problem.



回答3:

For me it was the exact same issue like mrbinky3000 has stated above: The server mod-rewrite was killing the hash in safari.

The solution was to use a full absolute link like: http://www.example.com/path/#item-1



回答4:

I just experienced an issue like this. I was using a url re-write in the asp.net web.config. When Safari the hash and everything after was removed. After trying some of the things mentioned above I was still having problems. The issue for me was that this was all happening under HTTPS. Once I specified the full URL in the redirect and included the https:// scheme the redirect worked correctly and preserved the hash. Note this wasn't an issue with Chrome or Firefox.



回答5:

I had a related problem with Safari (on iPhone/iOS) seemingly stripping off the hash/fragment when doing a:

var newHash = ...;
window.location.replace("#" + newHash);

The actually problem was a javascript error that only appeared on Safari. Since I could not easily assess a javascript console for the iPhone, I chose to download an old version of Window's Safari (related post, download).

Then, I could replicate the problem from the iPhone on my Window's desktop using the old version of Safari. At that point, I found a javascript tag had a missing ']'. This was a legitimate bug, but it was somehow ignored by Chrome, Firefox, and IE.

The window.location.replace() was not even being called, because the code was breaking out on the javascript error. So the problem wasn't that Safari was stripping the hashtag, even though it appeared that way from multi-browser testing.