Wednesday, June 5, 2024
176
rated 0 times [  181] [ 5]  / answers: 1 / hits: 38474  / 13 Years ago, sun, august 7, 2011, 12:00:00

I can't get window.location.hash = location.hash to work in Safari.



I'm using javascript to wrap the contents of my page with a scrollable DIV, placed below the navigation bar in my webpage. Since the scrollbar's location gets reset when the javascript runs, I'm losing the original hash location that the URL set. I need to re-cue the hash location without reloading the page using javascript, so I'm using window.location.hash = location.hash. It works in IE8, Firefox, and Opera, but it doesn't work in Safari. (I'll assume Chrome, too, but I haven't check). Any suggestions?



Hint: I like jQuery.


More From » google-chrome

 Answers
35

Webkit has two oddities that prevent window.location.hash = location.hash from working normally.




  1. Webkit responds to window.location.href instead of window.location.hash (like all the other browsers do). Curiously, webkit can still read the URL's hash tag using location.hash

  2. Webkit has a documented bug where the href location has to be set to the same location twice before the browser will go to the new location. Bug report here.



This code solved my problem: (using jQuery).



$(document).ready(function() {
gotoHASH()
};

function gotoHASH() {
if (location.hash) {
if ( $.browser.webkit == false ) {
window.location.hash = location.hash;
} else {
window.location.href = location.hash;
}
}
};

[#90767] Friday, August 5, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nadineannabellet

Total Points: 464
Total Questions: 94
Total Answers: 97

Location: Maldives
Member since Tue, Dec 21, 2021
3 Years ago
nadineannabellet questions
;