How do I test to see if links are external or internal? Please note:
- I cannot hard code the local domain.
- I cannot test for "http". I could just as easily be linking to my own site with an http absolute link.
- 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');
}
});