SiteKickr Web Development

Forcing external links to open in a new window with Javascript

This task becomes relatively easy with the help of our friend jQuery. The code below is almost too simple!

$('a').each(function() {
    url = $(this).attr('href');
  
    //detect external link
    if ( (url.indexOf('http') >= 0 || url.indexOf('https') >= 0)  &&  (url.indexOf('yoursitedomain.com') < 0))
        $(this).attr('target', '_blank');
});