Last active
September 16, 2023 14:54
-
-
Save heathcliff/7157933 to your computer and use it in GitHub Desktop.
getHashParam(), updateHashParam(), getParam()
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
function getHashParam(name) { | |
var regex = new RegExp("(#)("+name+")(\=)([^#]*)"), | |
matches = []; | |
matches = regex.exec(window.location.hash); | |
if (matches !== null && matches.length > 4 && matches[4] !== null) { | |
return matches[4]; | |
} else { | |
return false; | |
} | |
} | |
function updateHashParam(key, value) { | |
var hash = window.location.hash; | |
value = encodeURIComponent(value); | |
if (!getHashParam(key)) { | |
window.location.hash = window.location.hash + '#' + key + '=' + value; | |
return true; | |
} else { | |
var regex = new RegExp("(#)("+key+")(\=)([^#]*)"), | |
matches = []; | |
matches = regex.exec(window.location.hash); | |
if (matches !== null && matches.length > 0 && matches[0] !== null) { | |
window.location.hash = hash.replace(matches[0], '#' + key + '=' + value); | |
return true; | |
} | |
} | |
return false; | |
} | |
function getParam(name) { | |
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); | |
var regexS = "[\\?&]" + name + "=([^&#]*)"; | |
var regex = new RegExp(regexS); | |
var results = regex.exec(window.location.search); | |
if(results == null) { | |
return ""; | |
} else { | |
return decodeURIComponent(results[1].replace(/\+/g, " ")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment