Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.