How to open URL in a new window using JavaScript ?

In HTML, the anchor tag (<a>) is used to open new windows and tabs in a very straightforward manner. However, there are situations where you need to achieve the same functionality using JavaScript. This is where the window.open() method becomes useful.

The window.open() method is used to open a new browser window or a new tab, depending on the browser settings and the parameter values provided. Here’s how you can use it:

Syntax: 

window.open(URL, name, specs, replace);

Note: All the parameters are optional.

Approach

  • To open a URL in a new window, make sure that the second parameter is not _blank.
  • The other parameters can be varied accordingly as per the need of the new window.

Example 1: In this example, we are creating a button and setting an onclick function to open a new window.

HTML
<!DOCTYPE html>
<html>

<body style="text-align:center;">

    <h1 style="color:green">
        w3wiki
    </h1>

    
    <p>
        Click the button to
        open a new window.
    </p>


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

    <script>
        function NewTab() {
            window.open("https://www.w3wiki.net",
                    "", "width=300, height=300");
        }
    </script>
</body>

</html>

Output: 

Output

Example 2: Use Anchor tag to open URL in a new window.

HTML
<!DOCTYPE html>
<html>

<body style="text-align:center;">

    <h1 style="color:green">
        w3wiki
    </h1>


    <p>
        Click the button to open
        w3wiki in a new window.
    </p>


    <a onclick='window.open("https://www.w3wiki.net/",
            "_blank", "width=300, height=300");'>
        w3wiki
    </a>

</body>

</html>

Output:

Example 3: Use the Input tag to open the URL in a new window.

HTML
<!DOCTYPE html>
<html>

<body style="text-align:center;">

    <h1 style="color:green">
        w3wiki
    </h1>


    <p>
        Click the button to open
        w3wiki in a new window.
    </p>


    <input type="button" onclick="window.open(
'https://www.w3wiki.net/','Beginner',
'toolbars=0,width=300,height=300,left=200,top=200,scrollbars=1,resizable=1');"
           value="Open the window">

</body>

</html>

Output:


Output

Conclusion

Using JavaScript to open URLs in a new window or tab is a simple yet powerful functionality provided by the window.open() method. This method offers flexibility, allowing you to specify the URL, name, and features of the new window, making it suitable for a wide range of applications. Whether you need to enhance user experience by opening external links in new tabs or control the behavior and appearance of new browser windows, window.open() provides the necessary tools to achieve these goals effectively.

Supported Browsers:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari

HTML is the foundation of web pages and is used for webpage development by structuring websites and web apps. You can learn HTML from the ground up by following this HTML Tutorial and HTML Examples.

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.



Contact Us