I am working on a project in angular where I need to add dynamic links to canonical urls. So as the page changes the url in canonical href should also change/update.There are some conditions I have added because of which only parent Urls should be added. Below is the code:
var url = window.location.href,
urlSegments = url.split("/").length - 1 - (url.indexOf("http://")==-1?0:2);
if(urlSegments <= 7) {
var link = document.createElement('link');
link.setAttribute('rel', 'canonical');
link.setAttribute('href', url);
document.head.appendChild(link);
}
But this is not working.What wrong am I doing here and what can be the best solution for this. Thanks in advance!!