How do I use a jQuery not selector to select relative URLs?

Posted by Matt on Stack Overflow See other posts from Stack Overflow or by Matt
Published on 2010-05-07T09:55:54Z Indexed on 2010/05/07 10:18 UTC
Read the original article Hit count: 217

Filed under:
|
|
|
|

I'm working on a little jQuery script to add Google Analytics pageTracker onclick data to all relative URLs on my forum, allowing me to track clicks to external sites.

I don't want to add the onclick to internal links on forum.sitename or sitename, and I don't want to add them to any hrefs marked # or that start with /. My script below works nicely, but for one minor problem!

All of the forum's URLs are relative and don't start with /. I appear to have no way to change that, so need to modify the jQuery below to prevent it adding the onclick to links like as it currently does.

What I want to do, is to write a .not() function like .not("[href!^=http") to prevent jQuery from adding the onclick to any hrefs which do not start with http. However, .not() appears not to support this.

I'm new to jQuery and can't figure this out. Any pointers would be massively appreciated.

$(document).ready(function(){
     // Get URL from a href
     var URL = $("a").attr('href');

 // Add pageTracker data for GA tracking
 $("a")
 .not("[href^=#]")
 .not("[href^=http://forum.sitename]")
 .not("[href^=http://www.sitename]")
 .attr("onclick","pageTracker._trackEvent('Outgoing_Links', 'Forum', " + URL + ");")
 ;

});

Thanks!

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about google