techStackGuru

JavaScript Window


The window object is a window that contains a DOM element. The browser creates a window object for you automatically.

MethodDescription
alert()Displays the alert box.
prompt()Shows a dialog window to request user input.
open()Opens a new window.
confirm()Shows the confirm dialog box, which includes a message, an OK button, and a cancel button.
setTimeout()After a certain time has elapsed, the activity is carried out.
close()Closes the current window.

Lets take an example of alert() method here.


<!DOCTYPE html>
<html lang="en">
<body>
<script>
    
 function hello(){  
   alert("Hello Aura");  
 }

</script>     
<button type="button" onclick="hello();">Say Hello</button>
</body>
</html> 

Lets get window size in below example


<!DOCTYPE html>
<html lang="en">
<body>
    <script>
    function displayWindow(){
        var width = window.innerWidth;
        var height = window.innerHeight;
        alert("Width: " + width + " - " + "Height: " + height);
    }
    </script>
     
    <button type="button" onclick="displayWindow();">Dimension</button>
</body>
</html>                            

Lets take an example of prompt() method


<script type="text/javascript">  
  
 function display(){  
   var value= prompt("Enter Age");  
   alert("Your age : "+value);  
 }  

</script>  
  
<input type="button" value="Button" onclick="display()"/> 

Lets take an example of open() method


<script type="text/javascript"> 

 function display(){  
   open("https://www.techstackguru.com/javascript");  
 }  

</script>  
<input type="button" value="Website" onclick="display()"/> 

Lets take an example of confirm() method


<script type="text/javascript">  

function display(){  
var value= confirm("Confirm ?"); 

 if(value==true){  
   alert("Yes");  
 } else {  
   alert("No");  
 }   
}  
</script>  
  
<input type="button" value="button" onclick="display()"/>  

Lets take an example of setTimeout() method


<script type="text/javascript">  

 function display(){  
   setTimeout( function() {  
     alert("Triggered after 5 seconds")  
 }, 5000);  
}  
</script>  
  
<input type="button" value="Show" onclick="display()"/>  

previous-button
next-button