Created
August 29, 2013 14:03
-
-
Save sdurandeu/6378483 to your computer and use it in GitHub Desktop.
JS: Get and Set HashTag Parameter
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
$.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