Skip to content

Instantly share code, notes, and snippets.

@davidsword
Created April 19, 2018 16:07
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 davidsword/7900b193e44a8dd0660b8d9145c57cc8 to your computer and use it in GitHub Desktop.
Save davidsword/7900b193e44a8dd0660b8d9145c57cc8 to your computer and use it in GitHub Desktop.
JS - set and get hash query values from a URLs
// 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