The Lodge is members-only design/dev videos and Office Hours.

Next Office Hours Session: "Programming Fundamentals" Dec 02 - 2:00 PM Eastern

Find all Internal Links

Last updated on:

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

var siteURL = "http://" +;

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


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

    • Jenni
      Permalink to comment#

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

  2. 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() {

    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?

    • kougi
      Permalink to comment#

      why don’t you just use this :


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

We highly encourage you to post problematic HTML/CSS/JavaScript over on CodePen and include the link in your post. It's much easier to see, understand, and help with when you do that.

Markdown is supported, so you can write inline code like `<div>this</div>` or multiline blocks of code in in triple backtick fences like this:

  function example() {
    element.innerHTML = "<div>code</div>";

There's a whole bunch of content on CSS-Tricks.

Search for Stuff   •   Browse the Archives

Get the Newsletter ... or get the RSS feed