Skip to content

Instantly share code, notes, and snippets.

@clarkhacks
Last active May 17, 2024 07:18
Show Gist options
  • Save clarkhacks/e139f42caaf175844276bff69b41e282 to your computer and use it in GitHub Desktop.
Save clarkhacks/e139f42caaf175844276bff69b41e282 to your computer and use it in GitHub Desktop.
Retrieve Url Parameter Values.
function encodeHTML(s) {
return s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/"/g, '&quot;');
}
function ifPage(url, fx) {
if (window.location.pathname.slice(0, -5) == url) { // trailing ".html"
fx();
} else if (window.location.pathname == url) {
fx();
}
}
function paramVal(param) {
param = param.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + param + "(=([^&#]*)|&|#|$)"),
results = regex.exec(window.location.href);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
// url -> https://www.example.com/?ref=facebook
// code -> paramVal("ref") returns "facebook"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment