Open External Links In New Window in jQuery
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

$('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');
       });
   }
});

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

Or, you can still avoid the validation problems and just append the class target=_blank thing to any links with href attributes starting with http://. The example below only targets links in a #content area. Scoping down like that might be a good idea in case your menus are dynamic and create full URLs.

$("#content a[href^='http://']").attr("target","_blank");

Credits: CSS-TRICKS

You may want to check :

  1. Append Site Overlay DIV
  2. Meta Refresh / Redirect
  3. Form Submission Opens New Tab/Window
  4. Clean way to open popup windows
  5. force links to open in a new tab

Post a Comment

Your email is never shared. Required fields are marked *

*
*