JavaScript Open url in new tab

This quick JavaScript tutorial help to open a linked URL in a new browser window or tab. The HTML is anchor '<a>' tag that ll use to open a URL into the new tab using JavaScript.

HTML a target attribute provides an easy way to open a URL into the new browser window or tab. We’ll use _blank value in the target attribute to open the linked URL in a new tab or window.

<a href="https://phpflow.com" target="_blank">Visit Phpflow</a>

Above code, ll open phpflow.com into the new tab using pure HTML.

But If you want to open a URL with JavaScript. The JavaScript window.open() method is used to open a new browser window depending on the browser setting and the parameter values.

You can pass _blank as the second parameter into the window.open() method to open a URL in a new tab using JavaScript.

The following JavaScript code will open https://phpflow.com in a new browser tab or window.

window.open('https://phpflow.com', '_blank');

Full Source Code:

<html> 
  
<body> 
  
    <p>Click the button to open a new tab </p> 
  
    <button onclick="openUrl()"> 
      Open PhpFlow 
  </button> 
  
    <script> 
        function openUrl() { 
            window.open( 
              "https://phpflow.com", "_blank"); 
        } 
    </script> 
  
</body>

Leave a Reply

Your email address will not be published. Required fields are marked *