Skip to content

HIstory._updateHash not cross-browser friendly #1649

Closed
@ryanthegiantlion

Description

The following code is called to modify window.location.hash.

// Update the hash location, either replacing the current entry, or adding
// a new one to the browser history.
_updateHash: function(location, fragment, replace) {
if (replace) {
    location.replace(location.href.replace(/(javascript:|#).*$/, '') + '#' + fragment);
} else {
    location.hash = fragment;
}
}

Backbone passes the stripped down fragment without the leading hash and relies on the browser to add it. In other words, it expects the browser behaviour of

window.location.hash = "connections";

to add the leading hash so that the hash property stores "#connections" and not "connections".
There are two problems with this as far as I can tell: this behaviour is not a standard as far as I can tell (I checked the W3.org Window object working draft which is the latest doc I could find on it) and not all browsers implement this behaviour. In particular the Blackberry OS 5.0(which admittedly is not great) browser does not add the leading hash and this breaks things - Router.Navigate does not work as expected for instance,

Should the code perhaps be changed to something along the lines of "location.hash = "#" + fragment" which should work cross browser or alternatively should the fragment that is passed to the update function NOT be stripped of its '#' character?

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions