söndag 27 februari 2011

Code Snippet: Open External Links In New Window

You can do this straight with HTML, but that is invalid markup, this takes care of business without invalid code and unnecessary markup.

$('a').each(function() {
   var a = new RegExp('/' + window.location.host + '/');
   if(!a.test(this.href)) {
       $(this).click(function(event) {
           event.preventDefault();
           event.stopPropagation();
           window.open(this.href, '_blank');
       });
   }
});

Inga kommentarer:

Skicka en kommentar