Created
March 29, 2011 17:35
-
-
Save kostasdizas/892835 to your computer and use it in GitHub Desktop.
javascript hash query plugin
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
(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