Canonical script


Hey there

I’d like to revive a thread from the dead here and come back to canonical scripts as our tracking code appended to page URLs are being crawled as static URL’s and therefore flagging as duplicate content (title tags, meta descs, page content etc)

This old forum post has a mention to using script manager - which sounds like a great and potentially very simple solution - but what exactly would I need to put into the customer script to ensure every page has a canonical URL?

I am happy enough using the script manager but not sure what I would put exactly so that the script was dynamic in that it pulled the correct main page URL to use for each page…


Where are you pulling the dynamic url from?


See the [your URL goes here] section below. What code do I add there so that the global script can correctly pull the URL on each page?


Javascript for that would be


So off the cuff you’d want something along the lines of…

<link rel="canonical" href="anything" />
$("link[rel='canonical']").attr("href", window.location.href);

Should note I’ve not tested this at all and it would require jQuery… but it should grab the current url and make it
the canonical…


Thanks so much for the help! You legend!

Clearly you absolutely do understand what I am trying to achieve, so that part is sorted. I have applied this code and added it as a global script in the script manager area and what I get on a published page is:
(worth noting that the ‘anything’ is hyperlinked when I view source - and the word anything has been appended to the end of the correct url…

</script><link rel="canonical" href="anything" />
$("link[rel='canonical']").attr("href", window.location.href);


Okayyy try remove “anything” and replace with just “”


No luck… now its blank and no reference to the URL anywhere…
I notice too the Unbounce appears to add the tag before the <link rel… tag. Which may be causing the issue and I dont seem to have control over that.


Just had a chance to run that script in an empty page…see below…

Things might be a little weird between preview and published… and how you view the source of the page…
When I inspect the page and find canonical the image below shows… however if I “view frame source” I only see the “anything” as href…

[edit] I am running chrome fyi



You’re absolutely right. I have removed ‘anything’ from it as it appended that to the end of the URL - but like you I can see the correct URL when I inspect on Chrome but do not see it if I view source.

From the face of things, it looks like you have solved this for me! Thank you @TimothyDO!! :ok_hand: