20 - Using the Window Object

The browser window can be accessed using the window object in Javascript. The object has several properties and objects which can be used to perform some thrift and useful operations. Well, here are some of them.

window.alert()

The alert function can be used to pop up an alert with the message passed as a parameter to the function. In general it is usually a string that is customized to display what is required. Here are some examples

We have earlier seen many examples in all our earlier tutorials and used this function to understand our concepts.

window.prompt()

This function is different from the alert function in that it displays a pop up too but asks the user to input a value to a text box. You can pass a message that can help the user understand what has to be keyed in. On the press of the OK button the value is taken and returned by the function. If the cancel button is pressed then null is returned. This can be seen from the examples in the screen shots below.

The user inputs Javascript in the text box and presses ok. Now the value is taken and returned. In the code we have stored it in a variable and passed the variable to the alert function which displays it.

Now, time, the user can enter some value or leave it blank and press cancel. This would return a null value and the variable would have null value which can be seen.

window.print()

This function can be used to print the contents of the window that is currently being used.

In this example you can see that the prompt for choosing the printer and setting the printer options comes on when the window.print() statement is executed.