Grow your CSS skills. Land your dream job.

Last updated on:

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

    • Permalink to comment#

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

  2. 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?

    • Permalink to comment#

      why don’t you just use this :

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

  3. 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. 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. 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

Leave a Comment

Posting Code

Markdown is supported in the comment area, so you can write inline code in backticks like `this` or multiline blocks of code in in triple backtick fences like this:

```
<div>Example code</div>
```

You don't need to escape code in backticks, Markdown does that for you. If anything screws up, contact us and we can fix it up for you.

*May or may not contain any actual "CSS" or "Tricks".