How to open links in new tab or window using JQuery

Share this article

How to open links in new tab or window using JQuery. In this brief article I am going to show you how to open external links on your website in a new tab using JQuery. There are situations when you actually need this because you cannot go into html tag directly and make the necessary changes, and the alternate way that is available for you is going by JavaScript or jQuery to open the external links in a new window/tab.



The script is very simple and straight forward, it first checks whether the link starts with “http” and then it also check whether the link contains any words that are similar to our domain name to ensure that no internal links are getting triggered in a new tab/Window when clicked. This way we’re confident that only the external links are targeted to open in new tab/window.

Use the script below and you shall be good to go. Just replace the “” with your domain name.

<script src=""></script>
$(function() {
   $("a[href^='http']:not([href*=''])").each(function() {
       $(this).click(function(event) {
   , '_blank');

Did you Find it helpful?, drop a line and share it forward to help others. Have a nice day.