c

Using JS Objects with properties & Methods:

In the example your just saw, I displayed an alert box using a JS alert() method of the window object. It's purpose is to display pop-up boxes with some text and a button labeled "OK".

An object contain functions, which are known as methods when part of an object. methods are called using the basic syntax: objectName.methodName(). Any arguments required for the method are passed between the parentheses, just like a normal function call.

For example, the window object possesses a method named alert(), which simply displays an alert window.
                                    window.alert("This is an alert method in the form of a popup box");

Since it's not necessary to specify the window object we can leave it out from the code. Thus, the following code will function equally well as the one above.

                                    alert("This is an alert method in the for of a popup box");

In JS there are methods are available to:

  • open a window (window.open()),
  • write text to the html document (document.write),
  • get the sin of a number,
  • isolate the 1st letter in a word,
  • assign today's date to a variable (date=getDate() ),
  • send the user back to the previous page, or
  • display an alert box (window.alert()).

Tryit: Create an window alert method in the body section of an html document. Also, manipulate the document object using document methods. See table of methods for the document object.

Here are a few types of commands that methods are useful for.

  • Date Methods - set variables to the clock time in a variety of ways.
  • Window Methods - used to open and close new windows ( example Open a new window )
  • Document Methods - Generate new documents on the fly. ( example document Method)
  • Form Methods - select form items, send the cursor to text boxes and submit forms.
  • History Methods - Press the reader's 'Back' button and other tricks.
  • Text Methods - Define the look of your text variables before your display them.
  • Math Methods - sin, cos, round, random, absolute value, etc.
  • MessageBox Methods - Alert, Prompt, and Confirm are all methods.

See a table list of objects, methods, properties and event handlers.

 
Imsg
Discussion

Blogging

E-mail

 

JAVASCRIPT    -    LESSON 3
J s - m e t h o d s