Skip to content

Instantly share code, notes, and snippets.

@KuraFire
Last active August 11, 2021 20:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KuraFire/4f9deec8e2a2f3316a9e083f14c4bb07 to your computer and use it in GitHub Desktop.
Save KuraFire/4f9deec8e2a2f3316a9e083f14c4bb07 to your computer and use it in GitHub Desktop.
Remove a query parameter from the URL (e.g. fbclid) and replace the history with the cleaner URL
// Using jQuery
$(document).ready(function(){
// Remove fbclid
removeParam('fbclid');
function removeParam(parameter) {
let params = new URLSearchParams(document.location.search);
let urlParts = document.location.href.split('?');
let newUrl;
// Delete the passed parameter if present (no need to check)
params.delete(parameter);
if (params.toString() !== "") {
// If any other parameters were present, reappend
newUrl = urlParts[0] + '?' + params.toString()
} else {
newUrl = urlParts[0];
}
// Stripping a parameter should not push a new history state, hence replaceState
history.replaceState('', document.title, newUrl);
}
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment