Search

Open a URL in a new tab in jQuery

post-title

Generally, external links into website is opened into new tab so user remain into website for further reading. To open link into new tab, we use html attribute target="_blank" method.

Though we can also use Jquery to open link into new tab instead of current tab. In this article, I will show you how to redirect a page in a new tab using jquery.

In the below example, we open the Facebook page into new tab.

<!doctype html>
<html>
<head>
    <title>Open a link into new tab on click using jQuery</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
    <a href="https://www.facebook.com/LaravelCode" id="facebook">Facebook</a>
    <script type="text/javascript">
        $("#facebook").click(function(e) {
            e.preventDefault();
            window.open(this.href);
        });
    </script>
</body>
</html>
 

So save this file and open it into browser and click on the link. It should open into new tab. I hope it will help you.