JavaScript Window open() And close() Method Example


Share:
Published By: Admin, Published On: Nov 12, 2021, Category: Javascript


Hello Friends,

Now let's see example of how to use window open() and close() method example. Here you will learn how to use javascript window open() and close() method. This is a short guide on window open() and close() method. Let's get started with how to find window open() and close() method in javascript.

Here i will give you many example how you can use window open() and close() method javascript.

Example : 1
<html>
    <head>
        <title>JavaScript Window open() And close() Method Example - MyWebtuts.com</title>
    </head>
    <body>
        <h3>JavaScript Window open() And close() Method Example - MyWebtuts.com</h3>
        <button onclick="openWin()">Open New Window</button>
        <br><br>
        <button onclick="closeWin()">Close New Window</button>
        
        <script>
            var a1;

            function openWin() {
                a1 = window.open("", "myWindow");
                a1.document.write("<p>It is my 'New Window'</p>");
            }

            function closeWin() {
                a1.close();
            }
        </script>
    </body>
</html>    
Output :
Click on button it will redirect to another window :
It is my 'New Window'
Example : 2
<html> 
    <head> 
        <title>JavaScript Window open() And close() Method Example - MyWebtuts.com</title> 
    </head> 
    <body> 
        <h3>JavaScript Window open() And close() Method Example - MyWebtuts.com</h3>
        <b>Click the button to open MyWebtuts tutorial site</b><br>
        <button onclick="winOpen()"> Open MyWebtuts </button>
        <br><br> 
        <b>Click the button to close MyWebtuts tutorial site</b><br>
        <button onclick="winClose()"> Close MyWebtuts </button> 
        
        <script>
            var newWin;
            function winOpen() { 
                newWin = window.open( "https://www.mywebtuts.com/", "_blank");
            } 
         
            function winClose() { 
                newWin.close(); 
            } 
        </script> 
    </body>  
</html>  
Output :
Click on button it will redirect to another window :
MyWebtuts.com

It will help you...

Share: