Titles And Descriptions
Give every page a unique, descriptive title and meta description so searchers can pick the right result. JavaScript is allowed to set or change both. Our title links and snippets guide explains how Google uses them.
The Canonical Tag
The canonical tag tells Google which address is the main version of a page. The page code itself is the best place for it. If JavaScript touches it, it must set the same address as the original code, never a different one. If you cannot put it in the code, JavaScript may add it, but only if the original code has none. Google does not recommend this route, but it works, because Google reads the tag when it renders. A script might build the address from a clean name and add it to the head:
const link = document.createElement('link');
link.rel = 'canonical';
link.href = 'https://example.com/cats/' + cat.urlFriendlyName;
document.head.appendChild(link);
Make sure there is only ever one canonical tag. Do not add a second or change one that exists. The canonical URLs guide covers the rest.
Robots Meta Tags
A robots tag such as noindex, nofollow stops a page being indexed and its links followed. JavaScript can add or change it, for example to mark a product page as noindex when the product has gone, while telling the visitor it is no longer available. But there is a trap: when Google sees noindex in the original code, it may skip rendering, so a script that removes noindex may never run. If a page should be indexed, keep noindex out of the original code. The meta tags and noindex guide lists every tag.