How to use window.open() method In Javascript

  • To open a new tab, we have to use _blank in the second parameter of the window.open() method.
  • The return value of window.open() is a reference to the newly created window or tab or null if it failed.
  • Do not add a third parameter to it as it will result in the opening of a new window rather than a tab

Syntax:

window.open(URL, '_blank');

Example 1: In this example, we have used the above-explained approach.

HTML
<html>

<head>
    <title>Open URL in New Tab </title>
</head>

<body>


    <p> Click the button to open
        <b> w3wiki.org </b>
        in new tab
    </p>


    <button onclick="NewTab()">
        Open w3wiki
    </button>

    <script>
        function NewTab() {
            window.open(
"https://www.w3wiki.org", "_blank");
        }
    </script>

</body>

</html>

Output:

How to Open URL in New Tab using JavaScript ?

To open a URL in a new tab using JavaScript, utilize the window.open() method, providing the URL as the first argument and “_blank” as the second argument to specify opening in a new tab. This triggers the browser to open the URL in a new tab.

Similar Reads

Using window.open() method

To open a new tab, we have to use _blank in the second parameter of the window.open() method.The return value of window.open() is a reference to the newly created window or tab or null if it failed.Do not add a third parameter to it as it will result in the opening of a new window rather than a tab...

Using the anchor element (HTML DOM

Using the anchor element (HTML DOM) involves creating an ‘a’ element dynamically, setting its href attribute to the desired URL, and its target attribute to “_blank”. Finally, triggering a click event on the element opens the URL in a new tab....