in black and white
Main menu
Home About us Share a book
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics

Javascript for dummies 4th edition - Veer E.V

Veer E.V Javascript for dummies 4th edition - Wiley publishing , 2004. - 387 p.
ISBN: 0-7645-7659-3
Download (direct link): javascriptfordummies2005.pdf
Previous << 1 .. 26 27 28 29 30 31 < 32 > 33 34 35 36 37 38 .. 118 >> Next

Table 4-3 Accessing Image Properties
Property Name Value
document.companyLogo.src file:///C:/myPicture.jpg companyLogo
document.companyLogo.height 200
document.companyLogo.width 500
document.companyLogo.border 1
document.companyLogo.complete true
To see an example of this HTML and JavaScript code in action, take a look at
the ch4_properties.htm file located on the companion CD.
In the code snippets shown in Table 4-3, the name of each object property is fully qualified. If you’ve ever given a friend from another state driving directions to your house, you’re familiar with fully qualifying names — even if you’ve haven’t heard it called that before now. It’s the old narrow-it-down approach:
“Okay, as soon as you hit Texas, start looking for the signs for Austin. On the south side of Austin, you’ll find our suburb, called Travis Heights. When you hit Travis Heights, start looking for Sledgehammer Street. As soon as you turn onto Sledgehammer, you can start looking for 111 Sledgehammer. That’s our house.”
Chapter 4: Getting Acquainted with the Document Object Model
The JavaScript interpreter is like that out-of-state friend. It can locate and provide you with access to any property — but only if you describe that property by beginning with the most basic description (in most cases, the document object) and narrowing it down from there.
In Listing 4-1, the document object (which you create by using the HTML
<BODY> and </BODY> tags) contains the image called companyLogo. The companyLogo image, in turn, contains the properties src, name, height, width, border, and complete. That’s why you type Logo.srcto identify the src property of the image named companyLogo; or type document.companyLogo.widthto identify the width property; and so on.
Note, too, that in the HTML code in Listing 4-1, the values for src, name, height, width, and border are taken directly from the HTML definition for this object. The value of true that appears for the complete property, however, appears courtesy of your Web browser. If your browser couldn’t find and successfully load the myPicture.jpg file, the value of the complete property associated with this object would have been automatically set to false.
In JavaScript as in other programming languages, success is represented by true or 1; failure is represented by false or 0.
There's a method to this madness!
A method by any other name (some programmers call them behaviors or member functions) is a function that defines a particular behavior that an object can exhibit.
Take, for example, your soon-to-be-old friend the HTML button. Because you can click an HTML button, the button object has an associated method called the click() method. When you invoke a button’s click() method by using JavaScript, the result is the same as though a user clicked that button.
Unlike objects, properties, and event handlers, methods in JavaScript are always followed by parentheses, like this: click(). This convention helps remind programmers that methods often (but not always) require parameters. A parameter is any tidbit of information that a method needs in order to do its job. For example, the alert() method associated with the window object allows you to create a special kind of pop-up window (an alert window) to display some information on the screen. Because creating a blank pop-up window is pretty useless, the alert() method requires you to pass it a parameter containing the text that you want to display:
function checkTheEmailAddress () {
window.alertC'Sorry, the e-mail address you entered is not complete. Please try again.”)
80 Part I: Building Killer Web Pages for Fun and Profit
Some objects, like the built-in window object, are associated with scads of methods. You can open a window by using the open() method; display some text on a window by using the write() and writeln() methods; scroll a
window up or down by using the scroll(), scrollBy(), and scrollTo() methods; and so on.
Just as you do when referring to an object, a property, or an event handler, when you refer to a method in JavaScript you must preface that method with the specific name of the object to which it belongs. Table 4-4 shows you examples of how to call an object’s methods.
Table 4-4 Calling Object Methods
JavaScript Code Snippet What It Does
annoyingText.blink() Calls the blink() method associated with the string object. Specifically, it causes the string object called annoyingText to blink on and off.
self.frame1.focus() Calls the focus() method associated with
the frame object. Specifically, it sets the input focus to a frame called frame1 (which itself is associated with the primary document window).
document.infoForm.request Calls the click() method associated with the button object. Specifically, this code
clicks the button named requestForFree InfoButton, which is contained in the form called infoForm. (The infoForm form is contained in the primary HTML document.)
Previous << 1 .. 26 27 28 29 30 31 < 32 > 33 34 35 36 37 38 .. 118 >> Next