Skip to content

Instantly share code, notes, and snippets.

@krschmidt
Last active March 2, 2024 20:57
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save krschmidt/9d46d1d080454b55f8cf to your computer and use it in GitHub Desktop.
Save krschmidt/9d46d1d080454b55f8cf to your computer and use it in GitHub Desktop.
Set Canonical URL via Javascript
<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>
@cviebrock
Copy link

@shaheeriversion out of curiousity, how did you accomplish this with unhead in VueJS 3? I'm trying to set up a project and would love to get canonical link tags and og:url meta tags working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment