The JavaScript Diaries: Part 8

By Lee Underwood

In this installment we are going to take a look at the methods that are associated with the window object. Here we will look at what they are and how they work. Later on in this series we will work with them in greater detail. Until now, we've been learning how to build the framework of our scripts but now we'll start to see things happen! Using these methods you'll learn how to implement JavaScript in your Web sites.

Methods

To review, a method does something to the object or with the object. (Think of it as a "method" of accomplishing a specific task.) It's a command given to the object. A method can cause a new browser window to open, or it can cause text to be selected. According to the Core JavaScript Guide 1.5: "A method is a function associated with an object. You define a method the same way you define a standard function." Just like a function, a method also ends with a set of parentheses. If parameters are given to the method, just like the function, they will go inside the parentheses, but unlike a function, a method can act on a window object.

(You can download a zipped copy of the Core JavaScript Guide 1.5, written by Netscape developers. The Core JavaScript 1.5 Reference Manual can be viewed online or also downloaded in zip format.)

alert()

This method issues a command to open a popup window that displays a message to the visitor. We have already used this method in some of our previous sessions. The format is:

window.alert("How are you doing?");

The line of code tells the JavaScript interpreter to open a window within the current window environment displaying the text, "How are you doing?" Since the window object is the highest level object, making it the default object, it's permissible to drop the window portion and just use the method by itself. Also, in the case of the alert(), prompt(), and confirm() methods, they are not relative to any other window. The typical style of writing it would be:

alert("How are you doing?");

The format of the alert window itself, including the "OK" button, cannot be changed. You only have access to the message inside the window.

prompt()

This method issues a command to open a popup window that displays a prompt for the visitor to enter information. We have already used this method in some of our previous sessions. The format is:


window.prompt("Please provide your name","optional text");

The line of code tells the JavaScript interpreter to open a window within the current window environment and prompt the user to input data. The window will display the message text, "Please provide your name." The second parameter is optional and is used to suggest an entry in the input box. It will display the text in the input box of the window. Once again, since the window object is the highest level object, it's permissible to drop the window portion and just use the method by itself. The typical style of writing it would be:

prompt("Please provide your name","optional text");

Like the alert window, the format of the prompt window, including the "OK" and "Cancel" button, cannot be changed.

confirm()

This method issues a command to open a confirmation window for the visitor. We have already used this method in some of our previous sessions. The format is:

window.confirm("Are you sure you want to do this?");

As I said before, since the window object is the highest level object, it's permissible to drop the window portion and just use the method by itself. The typical style of writing it would be:

confirm("Are you sure you want to do this?");

As with the alert and prompt windows, the format of the confirmation window, including the "OK" and "Cancel" button, cannot be changed.




Page 1 of 6

 
1 2 3 4 5 6
Next Page

Make a Comment

Loading Comments...

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
  •  
  •  
  •  
Thanks for your registration, follow us on our social networks to keep up-to-date