Skip to content

Instantly share code, notes, and snippets.

@alassek
Last active December 10, 2015 20:58
Show Gist options
  • Save alassek/4491705 to your computer and use it in GitHub Desktop.
Save alassek/4491705 to your computer and use it in GitHub Desktop.
window.location sucks
/**
* `window.location` is a BAD api. Doesn't have a prototype, 'too much recursion' error
* if you try to inspect the constructor. Monkey-patching causes random disappearances
* of the monkey-patched function in Chrome, cloning causes 'too much recursion' in FF.
*
* This is what I'm reduced to. ಠ_ಠ
**/
(function () {
function Location () {
// Oh yeah, and Object.keys doesn't work on it, either.
'hash host hostname href pathname port protocol search'.split(' ').forEach(function (key) {
this[key] = window.location[key];
}, this);
}
function isPresent ( value ) {
return value && value.length > 0;
}
Location.prototype = {
toArray: function () {
return this.pathname.split('/').filter(isPresent);
}
}
App.location = new Location;
//history.js, if you are using pushState
History.Adapter.bind(window, 'statechange', function () {
App.location = new Location;
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment