views:

80

answers:

4

How do I test to see if links are external or internal? Please note:

  1. I cannot hard code the local domain.
  2. I cannot test for "http". I could just as easily be linking to my own site with an http absolute link.
  3. I want to use jQuery / javascript, not css.

I suspect the answer lies somewhere in location.href, but the solution evades me.

Thanks!


Update: Thanks to jAndy, I've got a slightly modified version of what he answered:

        hostname = new RegExp(location.host);
        // Act on each link
        $('a').each(function(){

            // Store current link's url
            var url = $(this).attr("href");

            // Test if current host (domain) is in it
            if(hostname.test(url)){
               // If it's local...
               $(this).addClass('local');
            }
            else if(url.slice(0, 1) == "#"){
                // It's an anchor link
                $(this).addClass('anchor'); 
            }
            else {
               // a link that does not contain the current host
               $(this).addClass('external');                        
            }
        });
A: 

Yes, I believe you can retrieve the current domain name with location.href. Another possibility is to create a link element, set the src to / and then retrieving the canonical URL (this will retrieve the base URL if you use one, and not necessarily the domain name).

Also see this post: http://stackoverflow.com/questions/2639070/get-the-full-uri-from-the-href-property-of-a-link

Savageman
+3  A: 
var comp = new RegExp(location.host);

$('a').each(function(){
   if(comp.test($(this).attr('href'))){
       // a link that contains the current host           
       $(this).addClass('local');
   }
   else{
       // a link that does not contain the current host
       $(this).addClass('external');
   }
});

Note: this is just a quick & dirty example. It would match all href="#anchor" links as external too. It might be improved by doing some extra RegExp checking.

jAndy
This works fairly well, although I'm going to hold off on an answer in case someone else has a more elegant solution. Out of curiosity, why do anchor links register as external?
Matrym
I'm pretty sure that this will not work with relative urls. `attr` is supposed to return the attribute, not the property (the property might be resolved, not the attribute).
Sean Kinsey
http://jsfiddle.net/zuSeh/ It is verified that this method does not work for relative urls.
Sean Kinsey
+1  A: 

And the no-jQuery way

var nodes = document.getElementsByTagName("a"), i = nodes.length;
var regExp = new RegExp("//" + location.host + "($|/");
while(i--){
    var href = nodes[i].href;
    var isLocal = (href.substring(0,4) === "http") ? regExp.test(href) : true;
    alert(href + " is " + (isLocal ? "local" : "not local"));
}

All hrefs not beginning with http (http://, https://) are automatically treated as local

Sean Kinsey
This is verified by the way
Sean Kinsey
This answer is more accurate. Relatives URL are also important.
Savageman
hah, someone actually downvoted this.. must have been a jQuery fanboy =)
Sean Kinsey
A: 
var external = RegExp('^((f|ht)tps?:)?//(?!' + location.host + ')');

Usage:

external.test('some url'); // => true or false
J-P