Skip to content

Instantly share code, notes, and snippets.

@davidvandenbor
Last active July 16, 2020 12:51
Show Gist options
  • Save davidvandenbor/67a04ea2a64eea9d0f0907b0eb32b61f to your computer and use it in GitHub Desktop.
Save davidvandenbor/67a04ea2a64eea9d0f0907b0eb32b61f to your computer and use it in GitHub Desktop.
Add canonical url via JS in html
<script type='text/javascript'>
var link = !!document.querySelector("link[rel='canonical']") ? document.querySelector("link[rel='canonical']") : document.createElement('link');
link.setAttribute('rel', 'canonical');
link.setAttribute('href', location.protocol + '//' + location.host + location.pathname);
document.head.appendChild(link);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment