Skip to content

Instantly share code, notes, and snippets.

@paulcushing
Last active April 8, 2022 15:28
Show Gist options
  • Save paulcushing/3fe46622b32b43ecde642c86c10d933c to your computer and use it in GitHub Desktop.
Save paulcushing/3fe46622b32b43ecde642c86c10d933c to your computer and use it in GitHub Desktop.
Parameter Pass-Through
// Catch the incoming params
const incomingQueryString = window.location.search.substring(1); // drops ? from the front;
// Capture all of the link clicks
document.addEventListener(`click`, (e) => {
// if it's an <a> link
if (e.target.closest(`a`) && incomingQueryString.length > 0) {
e.preventDefault();
// if it already has params, combine them, if not, add the new ones
window.location.href =
e.target.href +
(e.target.href.indexOf("?") === -1 ? "?" : "&") +
incomingQueryString;
}
});
@paulcushing
Copy link
Author

This just catches all of the parameters in the URL and appends them to the links on the page.

Particularly useful when you want to add a campaign parameter to an advertisement link that goes to a sales page and you want that parameter to continue through to the order form without having to manually add it to the sales page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment