Skip to content

Instantly share code, notes, and snippets.

@alassek
Last active Dec 10, 2015
Embed
What would you like to do?
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