Created
April 9, 2018 15:54
-
-
Save alexstone/ef88b8aa93bd618cffb3742c846dea36 to your computer and use it in GitHub Desktop.
Functions to help with fetching and updating parameters from the URI hash
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 parameters from the URI hash | |
|-------------------------------------------------------------------------- | |
*/ | |
function getParamsFromHash() { | |
var rawHash = window.location.hash.substr(1); | |
var hashParts = rawHash.split(';'); | |
var paramData = {}; | |
hashParts.forEach(function(part) { | |
partData = part.split(':'); | |
paramData[partData[0]] = partData[1]; | |
}); | |
return paramData; | |
} | |
/* | |
|-------------------------------------------------------------------------- | |
| Build and set the URI hash from param data | |
|-------------------------------------------------------------------------- | |
*/ | |
function buildHashFromParams(paramData) { | |
hashParts = new Array(); | |
for(key in paramData) { hashParts.push(key + ':' + paramData[key]); } | |
window.location.hash = hashParts.join(';'); | |
} | |
/* | |
|-------------------------------------------------------------------------- | |
| Update URI hash param | |
|-------------------------------------------------------------------------- | |
*/ | |
function updateHashParam(key, value) { | |
params = getParamsFromHash(); | |
params[key] = value; | |
buildHashFromParams(params); | |
return params; | |
} | |
/* | |
|-------------------------------------------------------------------------- | |
| Remove a URI hash param | |
|-------------------------------------------------------------------------- | |
*/ | |
function removeHashParam(key) { | |
params = getParamsFromHash(); | |
delete params[key]; | |
buildHashFromParams(params); | |
return params; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment