treehouse : what would you like to learn today?
Web Design Web Development iOS Development

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://" + top.location.host.toString();

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

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?

Leave a Comment

Use markdown or basic HTML and be nice.