Created
April 19, 2018 16:07
-
-
Save davidsword/7900b193e44a8dd0660b8d9145c57cc8 to your computer and use it in GitHub Desktop.
JS - set and get hash query values from a URLs
This file contains hidden or 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
// get the HASH values | |
function getHashParams() { | |
var hashParams = {}; | |
var e, | |
a = /\+/g, // Regex for replacing addition symbol with a space | |
r = /([^&;=]+)=?([^&;]*)/g, | |
d = function (s) { return decodeURIComponent(s.replace(a, ' ')); }, | |
q = window.location.hash.substring(1); | |
while (e = r.exec(q)) | |
hashParams[d(e[1])] = d(e[2]); | |
return hashParams; | |
} | |
// example.com/page/#showme=8&offset=10&hidden=true | |
// get a value from hash | |
getHashParams().showme == '8' | |
// set the hash | |
document.location.hash = 'showme='+wtv+'&offset='+wtv+'&hidden='+wtv; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment