×

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.

MethodsDescription
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

Related Topics

JavaScript Console log Multiple variables

What does console.log() do in JavaScript? The console.log() is a function in JavaScript language. It is used to: Print any message in the console which is visible to the userPrint the values...

3 minutes read.

JavaScript setTimeout()

The setTimeout() method creates a timer that, when it expires, runs a function or provides a piece of code. setTimeout() is an asynchronous function, which means that it will not interrupt...

3 minutes read.

JavaScript Function

An important part of JavaScript is the ability to create new functions within <script>.....</script> tag. To declare a function in JavaScript using function keyword. We call JavaScript function multiple times to reuse the...

3 minutes read.

Design a BMI calculator using JavaScript

BMI calculator BMI stands for Body Mass Indicator. It is a numeric value which is calculated based on the height and weight of a person. It represents the fatness of the...

4 minutes read.

JavaScript redirect URL with parameters

Data can be often passed between web pages as information in URL parameters or query strings. A URL (Uniform Resource Locator) can have a single parameter or multiple parameters.Parameters can...

4 minutes read.

Javascript Password Validation

Here we see that how to validate any password field. Like password field can be blank and length of password is minimum 8 characters. Example <!DOCTYPE html>   <html>   <head>   <script>   functionpass_validation()   {     var password=document.myform.password.value;     if (password==null || password=="")   {     alert("password can't be blank");     return false;     }   else if(password.length<8)   {     alert("Password must be at least 8 characters long.");     return false;       }     }     </script>   </head>   <body>   <form name="myform" method="post" action="register.php" onsubmit="return pass_validation()" >   Password: <input type="password" name="password">   <input type="submit" value="submit">   </form>   </body>   </html> Try Now ← Prev Next → ...

1 minute read.

JavaScript moment date difference

Often date differences are required on the web platforms for various reasons. They can be: To find the duration, of course, someone is pursuingTo find the age of a user from...

7 minutes read.

What is Ternary Operator in JavaScript?

In some cases, a ternary operator can be used instead of an if-else expression. A ternary operator examines a condition and then runs a block of code in response to...

4 minutes read.

Javascript loops

Loops in JavaScript: Loops are used to execute a specific piece or block of code repeatedly until the given condition is satisfied. What is a Loop? Almost all loops contain a certain...

7 minutes read.

Javascript Re-Password Validation

Example <!DOCTYPE html>   <html>   <head>   <script>   function pass_validation()   {   var firstpassword=document.f1.password1.value;     var secondpassword=document.f1.password2.value;     if(firstpassword==secondpassword){     return true;     }     else{   alert("Password does Not Match");     return false;     }     }    </script>   </head>   <body>   <form name="f1" action="/JavaScript/Index" onsubmit="return pass_validation()">   Password:<input type="password" name="password1" /><br/>   Re-enter :<input type="password" name="password2"/><br/>   <input type="submit">   </form>   </body>   </html> Try Now ← Prev Next → ...

1 minute read.

Javascript Email Validation

We can validate the email with the help of JavaScript. Here we check the condition related to any email id, like email it must have "@" and "." sign and...

2 minutes read.

Array to String in JavaScript

As a web developer, you often have to deal with multiple data types in a single application. How to convert an array to a string in JavaScript is essential for...

19 minutes read.

Javascript Print

JavaScript help's the functionality using the print function of window object. In JavaScript print function window.print() print the current web page when it executed. We can call function directly using onclick...

1 minute read.

Functional Programming in JavaScript

Most developers work with object-oriented programming while developing their applications, but sometimes they need to change their approach to solve some specific tasks. Although mostly we tend to go for...

6 minutes read.

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...

3 minutes read.

Javascript Dialog Box

A JavaScript dialog box is predefined function which is used to perform different task. Some functions are used in JavaScript Dialog box. FunctionDescriptionalert()To give alert message to userprompt()To input value from...

2 minutes read.

Javascript Document Object

Document object represents the whole HTML documents. When HTML document is loaded in browser it becomes the document object. It is the root elements that represent the HTML documents. With the help of...

2 minutes read.

Javascript Operators

The concept of operators is the basis of nearly all programming languages used in today's computing systems. Operators facilitate the execution of certain functions. For instance, the (+) symbol is...

8 minutes read.

Minify JavaScript

What do you mean by the term ‘Minification’? The technique of minification reduces the amount of code and markup in your websites and scripting files. It's one of the most used...

4 minutes read.

JavaScript dropdown onchange

What is onchange event? It is an event in JavaScript used to make the web pages dynamic. The onchange event gets triggered whenever the value of an event changes. It usually...

3 minutes read.