"window.location.hash = location.hash" does not work in Webkit (Safari & Chrome)

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;
        }
    }
};

I ended up with

window.location.hash = "";
window.location.hash = "myanchor";

This worked fine in all desktop browsers I tested in and on iOS and Android chrome.