Skip to content

JavaScript window objects

  • by

Whenever you open a tab in the browser automatically create a window object. Window objects are not JavaScript objects. JavaScript objects are strings, arrays, dates, etc.

JavaScript Window is a global Interface (object type) that is used to control the browser window lifecycle and perform various operations on it.

The important methods of window objects are as follows:

MethodDescription
alert()displays the alert box containing a message with the ok button.
confirm()displays the confirm dialog box containing a message with the ok and cancel button.
prompt()displays a dialog box to get input from the user.
open()opens the new window.
close()closes the current window.
setTimeout()performs an action after a specified time like calling function, evaluating expressions, etc.

JavaScript window objects example

Simple example code using window object alert() method with confirm() method.

<!DOCTYPE html>
<html>
<body>
  <script>  
    function show(){  
      var v = confirm("Do you want DELETE?");  

      if(v == true){  
        alert("ok");  
      }  
      else{  
        alert("cancel");  
      }  
      
    }  
  </script>  

  <input type="button" value="Delete Record" onclick="show()"/>  
</body>
</html>

Output:

JavaScript window objects

confirm()

<body>
  <script>  
    function msg(){  
      var v= confirm("Are u sure?");  
      if(v==true){  
        alert("ok");  
      }  
      else{  
        alert("cancel");  
      }  

    }  
  </script>  

  <input type="button" value="delete record" onclick="msg()"/>  
</body>

prompt()

<body>
  <script>  
    function msg(){  
      var v= prompt("Who are you?");  
      alert("I am "+v);  

    }  
  </script>  
  
  <input type="button" value="click" onclick="msg()"/>
</body>

open()

<body>
  <script>  
    function msg(){  
      open("https://tutorial.eyehunts.com/");  
    }  
  </script>  
  <input type="button" value="javatpoint" onclick="msg()"/>  
</body>

setTimeout()

<body>
  <script>  
    function msg(){  
      setTimeout(  
        function(){  
          alert("Welcome to Javatpoint after 2 seconds")  
        },2000);  
      
    }  
  </script>  
  
  <input type="button" value="click" onclick="msg()"/>   
</body>

Comment if you have any doubts or suggestions on this JS window topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading