1

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!!

Community
  • 1
  • 1
Ravikiran Bhat
  • 91
  • 1
  • 11

1 Answers1

0

You tagged it with angulajs tag so I think you should do it Angular way - with directive. You haven't specified what exactly is not working so hard to give any details, but here you have some example on how to manipulate <head> elements with a directive.

Community
  • 1
  • 1
Episodex
  • 4,479
  • 3
  • 41
  • 58