开发者

bug window.location.href + hash in Safari?

开发者 https://www.devze.com 2023-02-17 04:09 出处:网络
I have a script in my JavaScript file where I need to open a new file with a hash already set, something like:

I have a script in my JavaScript file where I need to open a new file with a hash already set, something like:

function search(queryString){
    window.location.href = "dosome.php#" + queryString
}

because dosome.php is the page开发者_JAVA技巧 where I have all the scripts for the search...

I know it sounds like a hack, but I cant spend more time rebuilding everything. I'm just trying to fix it temporarily.

It works in Firefox and Chrome, but for some reason, it doesn't work in Safari-- it doesn't send the URL with the hash. Safari sends:

domain.com/dosome.php

instead of

domain.com/dosome.php#queryvalues

What could be the problem?


If your server on dosome.php does some redirects, the hash is NOT retained, at least on Safari 4 and IE8. Chrome and Firefox work well.

In order to test this, try entering the URL http://yourdomain.com/...dosome.php#... in Safari's address bar and see if Safari keeps losing the #. (Test both with w/o www, even a simple www redirection loses the #.)

If this is the case, there is nothing you can do server-side because the #... is not sent to server; it's the client who is supposed to not lose it during navigation.


IS this done from the same page? Meaning, dosome.php?

Then try using location.hash,

location.hash = "#somestring";


I had a similar problem.

The following code was failing on an iPhone 5 in Safari:

window.location.href = 'http://example.com/result#somehash'

Safari was redirecting to just http://example.com/result/

It was working correctly in Chrome on the same phone as well as on the desktop.

I noticed it was adding the forward slash to the end and wondered if just adding a forward slash between the hash would work.

It did!

I changed my code to this and it worked:

window.location.href = 'http://example.com/result/#somehash'

Notice the / before the hash.

0

精彩评论

暂无评论...
验证码 换一张
取 消