Skip to content

Instantly share code, notes, and snippets.

@cliffordp
Last active October 28, 2024 15:18
Show Gist options
  • Save cliffordp/f4aa43badd4d99cee537a82ff0e5ab3b to your computer and use it in GitHub Desktop.
Save cliffordp/f4aa43badd4d99cee537a82ff0e5ab3b to your computer and use it in GitHub Desktop.
<script id="bizname-enforce-default-url-query-param">
// Source: https://gist.github.com/cliffordp/f4aa43badd4d99cee537a82ff0e5ab3b
// Related: https://youtu.be/MHKzHf1BhrY
// Function to check for the 'bizname' query parameter and redirect if not present.
(function enforceDefaultBiznameQueryParam() {
const urlParams = new URLSearchParams(window.location.search);
const bizname = urlParams.get('bizname');
// If 'bizname' query param is not present or is an empty string, redirect.
if (
(bizname === null || bizname.trim() === '')
&& '/only-here' === window.location.pathname // OPTIONAL if you want to restrict applying to a specific page.
) {
// Construct the new URL with the default 'bizname' query parameter.
const currentUrl = new URL(window.location.href);
currentUrl.searchParams.set('bizname', 'Your Business Name');
window.location.href = currentUrl.toString();
}
})();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment