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

  • Use Markdown, and it will escape the code for you, like `<div class="cool">`.
  • Use triple-backticks for blocks of code.
    ``` 
    <div>
      <h1>multi-line block of code</h1>
      <span>be cool yo.</span>
    </div>
    ```
  • Otherwise, escape your code, like <code>&lt;div class="cool"&gt;</code>. Markdown is just easier though.

Current ye@r *

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