A Web Design Community curated by Chris Coyier

Code Snippets Gallery

Code Snippets > jQuery > Find all Internal Links Submit one!

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^='#']");

2 Responses

  1. Jack says:

    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.

  2. LuK says:

    =), 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?

Leave a Comment

Remember:
  • Be nice.
  • Wrap multiline code in <pre> and <code> tags and escape it first (turn <'s into &lt;'s).
  • You may use regular HTML stuff like <a href="">, <em>, and <strong>
* This website may or may not contain any actual CSS or Tricks.