Last active
August 11, 2021 20:38
-
-
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
This file contains 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
// 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