Created
November 11, 2015 16:44
-
-
Save Gerst20051/02d076b37b100cf72d73 to your computer and use it in GitHub Desktop.
Hash Utils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.Hash = { | |
query: {}, | |
getHash: function () { | |
return decodeURIComponent(window.location.hash.substring(1)); | |
}, | |
clearHash: function () { | |
window.location.replace('#'); | |
}, | |
setHash: function (hash) { | |
window.location.replace('#' + encodeURI(hash)); | |
}, | |
get: function (key) { | |
if (this.has(key)) { | |
return this.query[key]; | |
} | |
}, | |
clear: function () { | |
this.query = {}; | |
this.update(); | |
}, | |
remove: function (key) { | |
delete this.query[key]; | |
this.update(); | |
}, | |
set: function (key, value) { | |
if (value !== undefined) { | |
this.query[key] = value; | |
this.update(); | |
return value; | |
} | |
}, | |
has: function (key) { | |
return this.query[key] !== undefined; | |
}, | |
parse: function () { | |
var that = this; | |
this.getHash().replace( | |
new RegExp('([^?=&]+)(=([^&]*))?', 'g'), | |
function ($0, $1, $2, $3) { | |
that.query[$1] = ($3 === undefined) ? null : $3; | |
} | |
); | |
return this.query; | |
}, | |
update: function () { | |
this.setHash('?' + $.param(this.query)); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment