Home » Web development » How to Open external links in a new window using Jquery

A simple jQuery trick to open all external links in your site in a new tab or window. First make sure you have jQuery active on your site. if you dont have Jquery in your Site then Download from http://jquery.com/. and then put below code just before


<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js" ></script>

<script type="text/javacript">
var $j = jQuery.noConflict();

$j(document).ready(function() {
//external attribute
    $j("a:not([@href*=http://YOURSITE.com/])").not("[href^=#]")
        .addClass("external")
        .attr({ target: "_blank" });
    }
);
</script>

That’s it, just make sure you change the http://YOURSITE.com to your website.

If you like you can remove the var $j = and replace all $j with simply just $

Array ( [0] => Web development )

Leave a comment

0 Comments.

Leave a Reply

You must be logged in to post a comment.