×

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 Forms Validation

Form validation works at server, after client had entered all the necessary details and then pressed submit button. JavaScript provides the facility to validate the form on the client side...

9 minutes read.

Javascript Page Redirect

The window.location object can be used to get the current page address (URL) and to redirect browser to new page. In some websites when we visit, we face a situation where we...

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 radio button checked value

What are radio buttons? A radio button is defined as an icon that is used to take input from the user. The user can choose only one option(value) from the group...

5 minutes read.

Javascript Cookies

Cookie is a piece of data which is sent from a website and stored locally by the user's browser. Cookie is needed because HTTP is stateless protocol. Today most of...

1 minute read.

Javascript Event

Events are things that happen, usually user action, that are associated with an object. All object have properties and methods. Some objects also have events. The event handler is a command that is...

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.

Javascript Variable

Variables are used to store values (name="Ram") or expressions (Sum=x+y). Before using of variable first we need to declare it. We use keyword var to declare a variable like this: var name; There are two types of variables: Local Variable ...

1 minute read.

Javascript Find Object In Array

What is find() method? It returns the value of the first element in the given array that fulfils the given testing function. However, if no values from the array are able...

3 minutes read.

JavaScript focus on input

In JavaScript, the elements can be focused using either focus() method or onfocus() event. What is focus() method? When we want to focus on an element (if it can be focused) or...

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.

D3.js Tutorial

Introduction of D3.js D3.js is referred to as a JavaScript library that is used to manipulate the documents according to the data. The intensity of D3 is based on web standards...

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

JavaScript Time Picker Demo

Time Pickers in JavaScript are lightweight and mobile-friendly controls that let the users enter or select date and time values. These values can be from a pop-up calendar or a...

5 minutes read.

Synchronous and Asynchronous in JavaScript

JavaScript is a powerful and versatile language that is used all over the web, from small websites to large applications. It is essential for developers to understand the differences between...

9 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 Image resize before upload

Image resizing can be a quite tedious task, so it is usually done on the server-side. The resized image file is then delivered to the client side. However, sometimes the...

3 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 Date

In JavaScript Date Object is data type build into the JavaScript language. Data object are created with the new Date(). JavaScript Date instance that represents a single moment in time. JavaScript...

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