Skip to content

Instantly share code, notes, and snippets.

@c3ry5
Created July 5, 2013 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save c3ry5/5933621 to your computer and use it in GitHub Desktop.
Save c3ry5/5933621 to your computer and use it in GitHub Desktop.
override backbone.js _updateHash to replace location.replace for old devices with window.history.replaceState
var oldBrowser = (reContainsAll([/mozilla\/5.0/i, /android/i, /applewebkit/i], navigator.userAgent)
|| reContainsAll([/mozilla\/5.0/i, /iphone os (5|4|3|2|1){1}/i, /applewebkit/i], navigator.userAgent))
&& !reContainsAll([/chrome/i], navigator.userAgent);
Backbone.History.prototype._updateHash = function(location, fragment, replace) {
var base = location.toString().replace(/(javascript:|#).*$/, '') + '#';
if (replace) {
if (oldBrowser) {
window.history.replaceState({}, document.title, base + fragment);
} else {
location.replace(base + fragment);
}
} else {
location.hash = fragment;
}
}
var reContainsAll = function (arr, str) {
var ret = true;
_.each(arr, function (val) {
if (!str.match(val)) {
ret = false;
return;
}
})
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment