1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Javascript for nofollow in external links

Discussion in 'JavaScript' started by chechogr, Apr 4, 2021.

  1. #1
    Hello, can someone recommend me a javascript to post a nofollow tag on each and every outgoing link on my site?

    Maybe you know this one friend: @JEET

    Thanks
     
    chechogr, Apr 4, 2021 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #2
    Is there a reason you need to use Javascript to do it?

    Here's a sample of how you could do it
    <HTML>
      <head>
    <style>
    body {font-family: arial;}
    a.altered {color: red;}
    </style>
       </head>
      <body>
        <ul>
          <li><a href='www.digitalpoint.com'>digitalpoint</a></li>
          <li><a href='abc.com'>abc</a></li>
          <li><a href='def.com'>def</a></li>
        </ul>
    
      </body>
    
    </html>
    HTML:
    var list = document.getElementsByTagName('a');
    
    for (var i = 0; i < list.length; i++) {
      console.log(list[i].href);
      if (list[i].href.includes("digitalpoint.com")) {
        //nothing to do
      } else {
      console.log(i);
        list[i].classList.add("altered");
        list[i].setAttribute('nofollow', 'nofollow');
      }
    }
    
    Code (JavaScript):
     
    sarahk, Apr 4, 2021 IP
  3. chechogr

    chechogr Active Member

    Messages:
    66
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #3
    The reason is the external links are generated into pages created by a plugin that do not exist in wordpress. I tried finding the place of the plugin that generates the links but couldn't find it.

    Is there a reason you copied html?

    The correct javascript is this one?


    var list = document.getElementsByTagName('a');

    for (var i = 0; i < list.length; i++) {
    console.log(list.href);
    if (list.href.includes("digitalpoint.com")) {
    //nothing to do
    } else {
    console.log(i);
    list.classList.add("altered");
    list.setAttribute('nofollow', 'nofollow');
    }
    }
     
    chechogr, Apr 5, 2021 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #4
    I gave you the html so you could reproduce my test on your own computer.
     
    sarahk, Apr 5, 2021 IP
  5. Daniel Campbell

    Daniel Campbell Banned

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #5
    Hi

    For certain links on your site, you might want to tell Google your relationship with. For regular links that you expect Google to follow without any qualifications, you might remove this attribute from links posted by members or users who to by an <a> tag), except nofollow , which is also available as robots meta tag.
     
    Daniel Campbell, Apr 5, 2021 IP
  6. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #6
    How does that help @chechogr?
     
    sarahk, Apr 5, 2021 IP
  7. chechogr

    chechogr Active Member

    Messages:
    66
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #7
    I added this code to my site's header and it broke:

    "var list = document.getElementsByTagName('a');

    for (var i = 0; i < list.length; i++) {
    console.log(list.href);
    if (list.href.includes("digitalpoint.com")) {
    //nothing to do
    } else {
    console.log(i);
    list.classList.add("altered");
    list.setAttribute('nofollow', 'nofollow');
    }
    }"
     
    chechogr, Apr 11, 2021 IP
  8. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #8
    what line did it break on?

    did you debug it, step through the code using the developers tools to see the values that variables have?
     
    sarahk, Apr 11, 2021 IP
    Efetobor Agbontaen likes this.
  9. Myles Claude

    Myles Claude Peon

    Messages:
    21
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    3
    #9
    You can make all external links no-follow using JavaScript, which tells the search engine and crawlers not to follow and crawl your hyperlinks and without using the “no-follow” tag, which allows the search engine crawlers to follow and crawl your hyperlinks.
     
    Myles Claude, Apr 12, 2021 IP
  10. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #10
    I suspect the OP knows that and it's why they're trying to make the change.
     
    sarahk, Apr 13, 2021 IP
  11. Efetobor Agbontaen

    Efetobor Agbontaen Active Member

    Messages:
    136
    Likes Received:
    41
    Best Answers:
    5
    Trophy Points:
    85
    #11
    These guys are just trying to get their post count up by writing random replies that is a bit related to the question
     
    Efetobor Agbontaen, Apr 16, 2021 IP
  12. Efetobor Agbontaen

    Efetobor Agbontaen Active Member

    Messages:
    136
    Likes Received:
    41
    Best Answers:
    5
    Trophy Points:
    85
    #12
    When you say "It broke", what do you mean?

    Did it give an error message?
    Did the entire Website just not open like "White screen of death"?
    Did the code itself give some error?

    Try to be specific.

    Also, a code like this should be put as the very bottom of the page. Remember it is querying all a tags. If you put it on the header, most of the a tags have probably not yet loaded.

    Or put your code here:
    document.addEventListener("DOMContentLoaded", function() {
      // Put your code here...
    });
    Code (JavaScript):
     
    Efetobor Agbontaen, Apr 16, 2021 IP
  13. Spoiltdiva

    Spoiltdiva Acclaimed Member

    Messages:
    7,732
    Likes Received:
    2,889
    Best Answers:
    53
    Trophy Points:
    520
    #13
    I'll never understand when people say this, as they often do. What possible good does having a large post count do for you? Believe me when I say that nothing helps. Not a big like number, post count, best answers, trophy points, member status,...nothing. You are only as good as your last post.
     
    Spoiltdiva, Apr 16, 2021 IP
  14. Efetobor Agbontaen

    Efetobor Agbontaen Active Member

    Messages:
    136
    Likes Received:
    41
    Best Answers:
    5
    Trophy Points:
    85
    #14
    Oh, it does help.
    It helps A LOT depending on why you are here.

    I invite you to read Digital Point rules and Policies etc. Do you still think it doesn't help?
     
    Efetobor Agbontaen, Apr 16, 2021 IP
  15. Spoiltdiva

    Spoiltdiva Acclaimed Member

    Messages:
    7,732
    Likes Received:
    2,889
    Best Answers:
    53
    Trophy Points:
    520
    #15
    I've been here for 10 years...the points mean nothing.
     
    Spoiltdiva, Apr 16, 2021 IP
  16. Efetobor Agbontaen

    Efetobor Agbontaen Active Member

    Messages:
    136
    Likes Received:
    41
    Best Answers:
    5
    Trophy Points:
    85
    #16
    I don't know about points but the post count of users gives more abilities in most forums including this one.

    The simplest example is you cannot post a link with < 10 post count on most forums. On some other forums like BHW, you cannot create a BST thread with < 100 post count. There are countless rules like that and DP also has some unless it was changed recently

    So saying high post count does no good is just NOT CORRECT
     
    Efetobor Agbontaen, Apr 16, 2021 IP
  17. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #17
    Which is HIGHLY relevant to this thread, given that nofollow on links is often done to make it so that people (or bots) who do spam pointless links get nothing out of it. #1 reason for mindless "pointless poster magnet" threads and replies is to get that sweet sweet post count up to where the ability to post links, images, PM's, or attachments become unlocked.

    That said, if the links are in fact being generated by JavaScript -- basically being inaccessible -- a LOT of search engines won't even recognize they exist in the first place. If you REALLY need to do this to them, you likely need to wait or hook so that any such scripting is run AFTER the one that's making them. The window LOAD event isn't going to provide that and might fire before, depending on where you place it in relation to the script making those links. Placing the script inside <head> is even WORSE in that regard, which is part of why 99.99% of the time you see <script> inside <head>, you're looking at those pesky 3i of web development. Ignorance, Incompetence, and Ineptitude.

    Which is really why NONE of this has ANY blasted business being done with client-side scripting. Neither these links nor setting nofollow on them. If you care at all about usability or accessibility, not a lick of this is any of JavaScript's flipping business!

    -- edit -- and @sarahk, it might help to use MODERN JavaScript. :D

    for (var a of document.getElementsByTagName("a")) {
    	if (a.href.includes("digitalpoint.com")) {
        //nothing to do
      } else {
        a.classList.add("altered");
        a.setAttribute("nofollow", "nofollow");
      }
    }
    Code (markup):
    Kills off "variables for nothing" and the slow-ish nature of array lookups.
     
    deathshadow, May 27, 2021 IP
    Efetobor Agbontaen likes this.
  18. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #18
    Oh, and just for laughs, if you absolutely positively need the fastest executing way to iterate through them.

    
    for (
    	let i = 0, anchors = document.getElementsByTagName('a'), a;
    	a = anchors[i];
    	i++
    ) {
    Code (markup):
    That's actually the best approach. On most websites you likely wouldn't have enough anchors for the speed difference to matter, but if it's something like a link farm page where you need to shave off every last possible millisecond per iteration, that's the GOAT.

    If raw speed doesn't matter, I'd use the for/of in my initial rewrite for clarity sake since "test on assignment" alone confuses rookies, much less using it as the test-case in a for loop. All good code is a balancing act, prioritizing what matters in the moment.

    Unless of course for some jacked-up whackadoodle reason you need support for older browsers that don't support for/of... which is basically IE at this point.
     
    deathshadow, May 28, 2021 IP
    Efetobor Agbontaen likes this.
  19. mmerlinn

    mmerlinn Prominent Member

    Messages:
    3,197
    Likes Received:
    818
    Best Answers:
    7
    Trophy Points:
    320
    #19
    And in those 10 years you have never closed your eye.
     
    mmerlinn, May 28, 2021 IP
  20. Spoiltdiva

    Spoiltdiva Acclaimed Member

    Messages:
    7,732
    Likes Received:
    2,889
    Best Answers:
    53
    Trophy Points:
    520
    #20
    Not entirely true as my avatar was an angel for about a year, and also a cute koala bear for about 6 months.

    *Isn't it just wonderful how we all stay on topic like we do?
     
    Spoiltdiva, May 28, 2021 IP