Learn JavaScript for Web Development

JavaScript is most versatile languages to code applications for the browser, desktop, phone or tablet. Write program for client browser or web server. Build interactive web site with JavaScript.

JavaScript - Window Object


  1. Window Object
  2. Properties of Window Object
  3. Methods of Window Object
  4. Example of 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, date etc.

Methods of window object

The important methods of window object are as follows:

MethodDescription
alert()displays the alert box containing message with ok button.
confirm()displays the confirm dialog box containing message with ok and cancel button.
prompt()displays a dialog box to get input from the user.
open()opens the new window.
close()closes the current window.
setTimeout()performs action after specified time like calling function, evaluating expressions etc.

Example of alert() in javascript

It displays alert dialog box. It has message and ok button.

See this example:

<script type="text/javascript">
function msg(){
alert("Hello Alert Box");
}
</script>
<input type="button" value="click" onclick="msg()"/>

Output:


Example of confirm() in javascript

It displays the confirm dialog box. It has message with ok and cancel buttons.

See this example:

<script type="text/javascript">
function msg(){
var v= confirm("Are u sure?");
if(v==true){
alert("ok");
}
else{
alert("cancel");
}

}
</script>

<input type="button" value="delete record" onclick="msg()"/>

Output:


Example of prompt() in javascript

It displays prompt dialog box for input. It has message and textfield.

<script type="text/javascript">
function msg(){
var v= prompt("Who are you?");
alert("I am "+v);

}
</script>

<input type="button" value="click" onclick="msg()"/>

Output of the above example


Example of open() in javascript

It displays the content in a new window.

<script type="text/javascript">
function msg(){
open("http://www.IndexSolutions.com");
}
</script>
<input type="button" value="IndexSolutions" onclick="msg()"/>

Output of the above example


Example of setTimeout() in javascript

It performs its task after the given milliseconds.

<script type="text/javascript">
function msg(){
setTimeout(
function(){
alert("Welcome to IndexSolutions after 2 seconds")
},2000);

}
</script>

<input type="button" value="click" onclick="msg()"/>

Output: