tags:

views:

122

answers:

1

I want to write a script which can determine whether a link is internal or external. This is simple from my perspective, all internal links are relative, so they start with a /. All external links start with an http:// - all good so far. However I can't figure out how to do a ':contains()' on anything other than text - how can a search for a string within an attribute?

Once I can do this I'm happy to add target _blank myself, unless you know a better way to do it

+4  A: 

You could use the attribute^=value syntax to find hrefs that start with http or /:

$('a[href^=http]') // external

$('a[href^=/]') // internal
Patrick McElhaney
Thanks, that seems to work as described. Cheers
chrism
Am I right in saying that an internal url won't always begin with a forward slash. it might be better to use $('a[href!=http:] a[href!=https:]') for internal.
kim3er