How do I redirect to a mobile site but keep the ha

2019-04-07 22:44发布

问题:

I'm looking to have it so that when a user loads our page it checks to see if they're on a mobile device and then it'll redirect them to a mobile version of our site, but keep the original hash tag from the link they followed. I've tried setting the new location with the hash tag in javascript and it works in Chrome but doesn't work in Safari. I've read that this is just something Safari does. Is there any work around to this?

回答1:

You should escape '#' symbol and everything would be OK



回答2:

We actually just got a very similar bug report from our customers.

For us the scenario involved a hashtag in the form of #quicklogin/abc123 and only appeared when viewing the site in Safari. This was part of the initial URL that the client would load and it would present them with an alternate login screen. When going directly to the URL in Safari, the browser removed the entire tag and ignored it.

When we changed this to something like #quicklogin/test (or any other hash containing only alpha characters) it worked fine. It also worked fine when loading the site and then manually applying the hash tag in two separate steps.

So our conclusion is that there may be something wrong with hashtags containing numeric values in Safari.



回答3:

You have to do this either by redirecting with javascript (because javascript can read the hash value) or by conditionally returning different html based on the user agent.

Browsers are supposed to preserve the hash fragment through a 302, but often don't (see 3 year old webkit bug below) and otherwise hash fragments are not sent to the server so they can't be dealt with manually.

https://bugs.webkit.org/show_bug.cgi?id=24175