Find all Internal Links

Find all links that start with the sites domain, a slash, relative file path, or a hashtag.

var siteURL = "http://" + top.location.host.toString();

var $internalLinks = $("a[href^='"+siteURL+"'], a[href^='/'], a[href^='./'], a[href^='../'], a[href^='#']");

Comments

  1. User Avatar
    Jack
    Permalink to comment#

    Surely this only works with wordpress though? What if I just had <a href=”about/img” rel=”nofollow”>? This wouldn’t be selected. You’d have to use this as an overwrite.

    • User Avatar
      Jenni
      Permalink to comment#

      No it’s just a Jquery script so you can use it widely in any website as you want.

  2. User Avatar
    LuK
    Permalink to comment#

    =), I use the following to find ALL EXTERNAL Links, for what kind of taks you want to find the INTERNAL LINKS?


    $(window).load( function() {
    $("a[href*='http://']:not([href*='"+location.hostname+"'])").attr('target','_blank');
    });

    but the problem I ran into was if the external Link has a https:// then it’s not recognized…how would I fix that in the above example?

    • User Avatar
      kougi
      Permalink to comment#

      why don’t you just use this :

      $(“[href=’http’]:not([href='”+location.hostname+”‘])”).attr(“target”,”_blank”);

  3. User Avatar
    Cosmin
    Permalink to comment#

    This will fail if you have a link like this:

    <a href="page.html">Link</a>
    

    Any solution to include such links as well?

  4. User Avatar
    Stefan Gruenwald
    Permalink to comment#

    LuK, why don’t you add both statements together (http and https), comma separated? I tried it, works beautifully.

        $("a[href*='http://']:not([href*='"+location.hostname+"']), a[href*='https://']:not([href*='"+location.hostname+"'])").attr('target','_blank');
    
  5. User Avatar
    Clément
    Permalink to comment#

    I use this :

    var internalLink = $("a:not([href^='http://']:not([href^='https://']");
    

    and I never use absolute url to target internal link

Submit a Comment

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

icon-closeicon-emailicon-linkicon-logo-staricon-menuicon-searchicon-staricon-tag