×

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 Tutorial

JavaScript is a light-weight, dynamic scripting language that is mainly employed to create interactive front-end web applications. It was originally developed in 1995 by Brendan Eich at Netscape Communications Corporation...

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

String object works with series of characters. It is used to store and manipulate text. There are two ways to create string in JavaScript: String literal. Using new keyword. String literal By using double quotes...

4 minutes read.

Javascript Math

Math is build-in object that has properties and methods for mathematical constants and functions. It allows you to perform mathematical tasks on numbers. Syntax varpi_val = Math.PI; varsin_val = Math.sin(30); Examples Math.pow() Math.pow(x,y) returns the value of x to the power...

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

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.

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.

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

JavaScript Arrays: JavaScript Arrays are a type of variables that allows us to store the individual or the group of values under a single variable. What is an Array in JavaScript? The array...

6 minutes 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 Object

An object is an entity having state and behavior. JavaScript is an object oriented scripting language. JavaScript is template based not class based but we can create object directly. Syntax to...

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

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 Button

What is a redirect button? When a button acts as a hyperlink, it is known as a redirect button. On clicking the button, it transfers the user to another page. How to...

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

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.

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.