Skip to content

Instantly share code, notes, and snippets.

@camsloanftc
Created October 17, 2021 18:37
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 camsloanftc/b2962c81b0069e290918a6ef1b0c9836 to your computer and use it in GitHub Desktop.
Save camsloanftc/b2962c81b0069e290918a6ef1b0c9836 to your computer and use it in GitHub Desktop.
Jquery - Append current url params to all urls on page
$("a").each(function () {
$(this).attr(
"href",
function () {
var currentUrlHasNoParams = location.search == ""
var hrefAlreadyHasQueryString = $(this).attr("href").includes("?")
var token = hrefAlreadyHasQueryString ? "&" : "?"
if(currentUrlHasNoParams){
return $(this).attr("href")
}
return $(this).attr("href") + token + location.search.substring(1)
}
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment