Last active
July 21, 2022 08:48
-
-
Save abid112/124be6b490ffdd24f4ee8e7aa7d6826c to your computer and use it in GitHub Desktop.
Change URL based on URL Parameter using jQuery
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
//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