JavaScirpt Tutorial Index

JavaScript Tutorial Javascript Example Javascript comment Javascript Variable Javascript Data Types Javascript Operators Javascript If Else Javascript Switch Statement Javascript loop JavaScript Function Javascript Object JavaScript Arrays Javascript String Javascript Date Javascript Math Javascript Number Javascript Dialog Box Javascript Window Object Javascript Document Object Javascript Event Javascript Cookies Javascript getElementByID Javascript Forms Validation Javascript Email Validation Javascript Password Validation Javascript Re-Password Validation Javascript Page Redirect Javascript Print

Misc

JavaScript P5.js JavaScript Minify JavaScript redirect URL with parameters Javascript Redirect Button JavaScript Ternary Operator JavaScript radio button checked value JavaScript moment date difference Javascript input maxlength JavaScript focus on input Javascript Find Object In Array JavaScript dropdown onchange JavaScript Console log Multiple variables JavaScript Time Picker Demo JavaScript Image resize before upload Functional Programming in JavaScript JavaScript SetTimeout() JavaScript SetInterval() Callback and Callback Hell in JavaScript Array to String in JavaScript Synchronous and Asynchronous in JavaScript Compare two Arrays in JavaScript Encapsulation in JavaScript File Type Validation while Uploading Using JavaScript or jquery Convert ASCII Code to Character in JavaScript Count Character in string in JavaScript Get First Element of Array in JavaScript How to convert array to set in JavaScript How to get current date and time in JavaScript How to Remove Special Characters from a String in JavaScript Number Validation in JavaScript Remove Substring from String in JavaScript

Interview Questions

JavaScript Interview Questions

Javascript Window Object

JavaScript Window Object

The window object represents a window in browser. An object of window is created automatically by the browser. Window is the object of browser, it is not the object of JavaScript. The JavaScript objects are string, array, data etc. It is used to display the popup dialog box such as alert box, confirm dialog box, input dialog box etc. The window methods are mainly for opening and closing new windows.

Methods of window object

The following are the main methods.
Methods Description
alert() It display the alert box containing message with ok button.
confirm() It displays the confirm dialog box which contain message with ok and cancel button.
prompt() It display a dialog box to get input from the user.
Open() Open new window.
Close() Close current window.
setTimeout() It performs action after specified time like calling function, evaluating expressions etc.

Example of alert()

<!DOCTYPE html>  
<html>  
<body>  
<script type="text/javascript">  
function msg(){    
alert("Alert Box");    
}    
</script>  
<input type="button" value="click" onclick="msg()"/>  
</body>  
</html>
Try Now

Example of confirm()

<!DOCTYPE html>  
<html>  
<body>  
<script type="text/javascript">  
function msg(){    
var a= confirm("Are u sure?");    
if(a==true){    
alert("ok");    
}    
else{  
alert("cancel");    
}    
}    
</script>  
<input type="button" value="Delete record" onclick="msg()"/>  
</body>  
</html>
Try Now

Example of prompt()

<!DOCTYPE html>  
<html>  
<body>  
<script type="text/javascript">  
function msg(){    
var x= prompt("Who are you?");    
alert("I am "+x);    
}    
</script>  
<input type="button" value="click" onclick="msg()"/>  
</body>  
</html>
Try Now

Example of open()

<!DOCTYPE html>  
<html>  
<body>  
<script type="text/javascript">  
function msg(){    
open("https://www.tutorialandexample.com");    
}    
</script>  
<input type="button" value="tutorialandexample" onclick="msg()"/>  
</body>  
</html>
Try Now

Example of setTimeout()

<!DOCTYPE html>  
<html>  
<body>  
<script type="text/javascript">  
function msg(){    
setTimeout(  
function(){    
alert("Welcome to tutorialandexample after 2 seconds")    
},2000);     
}    
</script>  
<input type="button" value="click" onclick="msg()"/>  
</body>  
</html>
Try Now