Following on from Jaspers comment on my blog post regarding my “newly discovered” way of creating new windows on links without using the target=”_blank” attribute, he posted a tidy bit of code based on Mootools that detects all <a> tags on his page, then opens in a new window if it starts with http:// (indicating it being an external link).
That’s cool. So I thought I would do the same using my preferred Javascript framework, jQuery. Here’s my code which has now been implemented on this site & looks like its working just fine…
$('.entry a').each(function(){
if ($(this).attr('href').substr(0,7) == 'http://')
{
$(this).addClass('new-window');
}
});
What we’re doing - top line, cycle through all <a> tags within the .entry class. Secondly, if the first 7 characters of the Href attribute within our <a> tag equals http://, then add a class to the <a> tag called new-window.
This will then be detected by my other small script on my page, as described here. Job done!





5 comments so far, what do you think? ...