Javascript add nofollow

Hi guys this code adds nofollow to all outgoing links. How can I make it skip some of the URLs like:

Facebook
Twitter
Google+

function nofollow(){

   $script = '<script type="text/javascript">';
   $script .= "$('a').each(function() {
                  var a = new RegExp('/' + window.location.host + '/');
                  if(!a.test(this.href)) {
                     $(this).attr('rel','nofollow');
                  }
              });
              </script>";
              
      
    OW::getDocument()->appendBody($script);
    
}

add them to the RegExp.

I did this but it’s not working:

$script .= "$('a').each(function() {
       
                  var a = new RegExp('/' + window.location.host + '/');
                  var f = new RegExp('https://www.facebook.com/amigotecom');
                  var g = new RegExp('https://plus.google.com/+Amigote');
                  var t = new RegExp('https://twitter.com/amigotecom');

                  if(!a.test || !f.test || !g.test || !t.test (this.href)) {
                     $(this).attr('rel','nofollow');
                  }
              });
              </script>";

if you don’t call the test() method properly, it sure doesn’t work. (i.e. you must pass the href to each test call, not only to the last)

Please would you mind post the solution case I am not really an expert please?

This one doesn’t work either please help?

if(!f.test(this.href) || !t.test(this.href)) 
                  {
                     $(this).attr('rel','nofollow');
                  }

have you checked, that each RegExp separately/for itself works?

Separately each works except Google case of the + symbol.

This one seem to work:

if(!a.test(this.href)) 
                  {
                     if(!f.test(this.href))
                     {
                         if(!t.test(this.href)) 
                         {
                             if(!g.test(this.href))
                             {
                     $(this).attr('rel','nofollow');
                  }
                 }
                }
               }

How to fix Google plus link case it has + symbol?

+ is a special character in RegExp, you need to escape it.

that means you need to use logical and, not logical or.

I would assume this would not affect the search engines as they would see the HTML the way it looks before the JavaScript runs. Just what is it you are trying to achieve by doing this as there is almost certainly a better way.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.