Skip to content

Instantly share code, notes, and snippets.

@Gerst20051
Created November 11, 2015 16:44
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 Gerst20051/02d076b37b100cf72d73 to your computer and use it in GitHub Desktop.
Save Gerst20051/02d076b37b100cf72d73 to your computer and use it in GitHub Desktop.
Hash Utils
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