Skip to content

Instantly share code, notes, and snippets.

@kostasdizas
Created March 29, 2011 17:35
Show Gist options
  • Save kostasdizas/892835 to your computer and use it in GitHub Desktop.
Save kostasdizas/892835 to your computer and use it in GitHub Desktop.
javascript hash query plugin
(function ($) {
$.hashQuery = {
get: function (key) {
d = {};
$.each(window.location.hash.slice(1).split("&"), function (b, a) {
a !== "" && (a.indexOf("=") !== -1 ? (a = a.split("="), d[a[0]] = a[1]) : d[a] = "")
});
if ($.isEmptyObject(d)) {
return !1
}
if (key !== undefined) {
return d[key];
} else {
return d;
}
},
remove: function(key) {
var arr = [];
$.each(window.location.hash.slice(1).split("&"), function (b, a) {
var string = a.split("=")[0].toLowerCase();
if (string !== key) {
arr.push(a);
}
});
window.location.hash = arr.join("&");
},
add: function (key, value) {
var h = window.location.hash.slice(1);
if (h !== "") {
h += "&";
}
h += key + "=" + value;
window.location.hash = h;
},
replace: function(key, value) {
this.remove(key);
this.add(key, value);
}
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment