Skip to content

Instantly share code, notes, and snippets.

@abid112
Last active July 21, 2022 08:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abid112/124be6b490ffdd24f4ee8e7aa7d6826c to your computer and use it in GitHub Desktop.
Save abid112/124be6b490ffdd24f4ee8e7aa7d6826c to your computer and use it in GitHub Desktop.
Change URL based on URL Parameter using jQuery
//Trim and get the value of URL parameter from URL
var getUrlParameter = function getUrlParameter(sParam) {
var sPageURL = window.location.search.substring(1),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
}
}
return false;
};
//Set parameter value in a variable. replace "id" with your parameter name.
var url_id = getUrlParameter('id');
//Check the value and chnage the URL of any <a> tag. Replace "eael-creative-button" with your class name or ID
if (url_id == 'test') {
jQuery(".eael-creative-button").attr("href", "https://google.com");
}
if (url_id == 'test2') {
jQuery(".eael-creative-button").attr("href", "https://crisp.com");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment