Last active
April 8, 2022 15:28
-
-
Save paulcushing/3fe46622b32b43ecde642c86c10d933c to your computer and use it in GitHub Desktop.
Parameter Pass-Through
This file contains hidden or 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
// 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; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.