Open In App

JavaScript window.open() Method

The Javascript window.open() method is used to open a new tab or window with the specified URL and name. It supports various parameters that can be used to specify the type and URL location of the window to be opened.

Syntax:



window.open(url, windowName, windowFeatures)

Parameters: It has the following parameters as mentioned above and described below: 

Example 1: In this example, we will open a new window on a button click with the specified parameters.






<!DOCTYPE html>
 
<head>
    <style>
        body {
            background-color: #272727;
            display: flex;
            justify-content: center;
            align-items: center;
            margin-top: 20%;
        }
 
        .main {
            width: 50%;
            height: 100%;
            display: flex;
            justify-content: center;
            align-items: center;
            text-align: center;
            flex-direction: column;
            background-color: rgb(82, 82, 82);
            margin: 10px;
            font-family: 'Roboto', sans-serif;
        }
 
        .btn {
            width: 100%;
            height: 50px;
            background-color: rgb(29, 29, 29);
            color: white;
            font-size: 20px;
            border: none;
            cursor: pointer;
        }
    </style>
</head>
 
<body>
    <div class="main">
        <h1>Click the button to open a new window</h1>
        <button class="btn">Open</button>
    </div>
    <script>
        document.querySelector('.btn')
            .addEventListener('click', function () {
                window.open('https://www.geeksforgeeks.org/',
                    '_blank',
                    'width=400,height=400 top=200,left=600');
            });
    </script>
</body>
 
</html>

Output:

 

Example 2: In this example, we will open a blank window by passing the URL as an empty string.




<!DOCTYPE html>
 
<head>
    <style>
        body {
            background-color: #272727;
            display: flex;
            justify-content: center;
            align-items: center;
            margin-top: 20%;
        }
 
        .main {
            width: 50%;
            height: 100%;
            display: flex;
            justify-content: center;
            align-items: center;
            text-align: center;
            flex-direction: column;
            background-color: rgb(82, 82, 82);
            margin: 10px;
            font-family: 'Roboto', sans-serif;
        }
 
        .btn {
            width: 100%;
            height: 50px;
            background-color: rgb(29, 29, 29);
            color: white;
            font-size: 20px;
            border: none;
            cursor: pointer;
        }
    </style>
</head>
 
<body>
    <div class="main">
        <h1>Click the button to open a new window.</h1>
        <button class="btn">Open</button>
    </div>
</body>
 
<script>
    document.querySelector('.btn')
    .addEventListener('click', function () {
        window.open('',
                    '_blank',
                    'width=400,height=400 top=200,left=600');
    });
</script>
 
</html>

Output:

 

Supported Browsers:

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.

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.


Article Tags :