Skip to content

Instantly share code, notes, and snippets.

@sdurandeu
Created August 29, 2013 14:03
Show Gist options
  • Save sdurandeu/6378483 to your computer and use it in GitHub Desktop.
Save sdurandeu/6378483 to your computer and use it in GitHub Desktop.
JS: Get and Set HashTag Parameter
$.getHashtagParam = function(paramToRetrieve: string): string {
var params = (document.URL.split("#")[1] || "").split("&");
var paramsCount = params.length;
for (var paramIndex = 0; paramIndex < paramsCount; paramIndex = paramIndex + 1) {
var singleParam = params[paramIndex].split("=");
if (singleParam[0] === paramToRetrieve) {
return singleParam[1];
}
}
}
$.setHashtagParam = function(paramToSet: string, value: string): void {
var hashtag = document.location.hash.split("#")[1];
var params = hashtag ? hashtag.split("&") : [];
var paramsCount = params.length;
var paramFound = false;
for (var paramIndex = 0; paramIndex < paramsCount; paramIndex = paramIndex + 1) {
var singleParam = params[paramIndex].split("=");
if (singleParam[0] === paramToSet) {
params[paramIndex] = singleParam[0].concat("=", value);
paramFound = true;
}
}
if (!paramFound) {
params.push(paramToSet.concat("=", value));
}
document.location.hash = params.join('&');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment