|Language:||English, Spanish, Japanese|
|ePub File Size:||20.43 MB|
|PDF File Size:||17.11 MB|
|Distribution:||Free* [*Regsitration Required]|
Tip In practice. Notice that the line of text inside the parentheses is contained within quotation marks. Instead of using new Object. Click here to view code image if input. Strings A string is a sequence of characters from within a given character set for example. Note the break statement after each case. As with new nodes created by createElement and createTextNode.
The window object has various child objects. Any HTML page loaded into the browser creates a document object containing all of the HTML and other resources that go into making up the displayed page.
All of these offer excellent support for the DOM. Apple Safari. The first child object shown in Figure 1. Note The use of early browsers such as Netscape Navigator any version and Internet Explorer up to version 5. The window and document Objects Each time your browser loads and displays a page.
These objects. In the DOM. This object is a parent or ancestor to everything else in the DOM representation of your page. We look in detail at these objects in Hour 4. We can simply use window.. For example object1. Object Notation The notation we use to represent objects within the tree uses the dot or period: Tip The window object always contains the current browser window. As a shortcut.
We begin with two methods. The window object. Here we are calling the alert method of the window object. If you have several windows open. Tip In practice. To refer to one of these documents. When you call the alert method. So Click here to view code image. Notice that the line of text inside the parentheses is contained within quotation marks.
Caution The default security settings in some browsers cause them to show a security warning when they are asked to open local content. Caution Some text editor programs might try to add a. Save it to a convenient place on your computer.
Most definitely. Neither d. A compiled language b.
An interpreted language c. The document object. The document method c. Quiz 1. Other than in extreme circumstances.
After the code has been read and executed. A comment written using this syntax can span multiple lines: To ease the readability of your code. Click here to view code image this is statement 1. We refer to such lines as comments. To add a multiline comment in this way. A comment that occupies just a single line of code can be written by placing a double forward slash before the content of the line: Comments can act as reminders to you.
We can use variable names in our operations too: Tip If you need to increment or decrement a variable by a value other than one.
Creating an Image Rollover We can use the onMouseOver and onMouseOut events to change how an image appears while the mouse pointer is above it. The code is shown in Listing 2. You can change the image names tick. You should see that the image changes as the mouse pointer enters. In this example we used two images.
Exercises Starting with Listing 2. Instead, add an onClick handler to set the.
Using functions also makes your code easier to debug and maintain. There may be 50 places in your code where such calculations are carried out. However, if all such calculations are wrapped up in a few functions used throughout the application, then you just need to make changes to those functions. Your changes will automatically be applied all through the application.
Listing 3. The result of clicking the button is shown in Figure 3. Passing Arguments to Functions It would be rather limiting if your functions could only behave in an identical fashion each and every time they were called, as would be the case in the preceding example. Fortunately, you can extend the capabilities of functions a great deal by passing data to them. You do this when the function is called, by passing to it one or more arguments: Now we can call our function, replacing the variable x with a number.
Calling the function like the following results in a dialog box being displayed that contains the result of the calculation, in this case Of course, you could equally pass a variable name as an argument. The following code would also generate a dialog containing the number Multiple Arguments Functions are not limited to a single argument. When you want to send multiple arguments to a function, all you need to do is separate them with commas: You can use as many arguments as you want.
The names that you give to arguments are only used inside the function definition to specify how it works. We talk about this in more detail later in the hour when we discuss variable scope. The function buttonReport takes three arguments, those being the id, name, and value of the button element that has been clicked. With each of these three pieces of information, a short message is constructed.
These three messages are then concatenated into a single string, which is passed to the alert method to pop open a dialog containing the information. Such a prefixed character is known as an escape sequence. We need to add an onClick event handler to this button from which to call our function. The complete listing is shown in Listing 3. Use your editor to create the file buttons. You should find that it generates output messages like the one shown in Figure 3.
Returning Values from Functions OK, now you know how to pass information to functions so that they can act on that information for you.
But how can you get information back from your function? Luckily, there is a mechanism to collect data from a function call—the return value.
Instead of using an alert dialog within the function, as in the previous example, this time we prefixed our required result with the return keyword. To access this value from outside the function, we simply assign to a variable the value returned by the function: Note The values returned by functions are not restricted to numerical quantities as in this example.
Tip Where a function returns a value, we can use the function call to pass the return value directly to another statement in our code. The value of 27 returned from the function call cube 3 immediately becomes the argument passed to the alert method. Scope of Variables We have already seen how to declare variables with the var keyword.
There is a golden rule to remember when using functions: If we run this code, we first see an alert dialog with the value of the variable invoiceValue which should be 55, but in fact will probably be something like We will not, however, then see an alert dialog containing the value of the variable total.
This is because we placed the declaration of the variable total inside the addTax function. We used the return keyword to pass back just the value stored in the variable total, and that value we then stored in another variable, invoice. We refer to variables declared inside a function definition as being local variables; that is, local to that function. Variables declared outside any function are known as global variables.
To add a little more confusion, local and global variables can have the same name, but still be different variables! The range of situations where a variable is defined is known as the scope of the variable—we can refer to a variable as having local scope or global scope.
Within the showVars function we manipulate two variables, a and b. The variable a we define inside the function; this is a local variable that only exists inside the function, quite separate from the global variable also called a that we declare at the very beginning of the script. The variable b is not declared inside the function, but outside; it is a global variable.
When the page is loaded, showVars returns a message string containing information about the updated values of the two variables a and b, as they exist inside the function—a with local scope, and b with global scope.
Finally, you learned about the local or global scope of a variable, and how the scope of variables affects how functions work with them. Can one function contain a call to another function? Most definitely; in fact, such calls can be nested as deeply as you need them to be. What characters can I use in function names? Function names must start with a letter or an underscore and can contain letters, digits, and underscores in any combination.
They cannot contain spaces, punctuation, or other special characters. Functions are called using a. The function keyword b.
The call command c. The function name, with parentheses 2. What happens when a function executes a return statement?
An error message is generated. A value is returned and function execution continues. A value is returned and function execution stops. A variable declared inside a function definition is called a. A local variable b. A global variable c. An argument.
A function is called using the function name. After executing a return statement, a function returns a value and then ceases function execution. Exercises Write a function to take a temperature value in Celsius as an argument, and return the equivalent temperature in Fahrenheit, basing it on the code from Hour 2.
Test your function in an HTML page. Hour 4. We also looked at one of its child objects, document. In this hour, we introduce some more of the utility objects and methods that you can use in your scripts.
Interacting with the User Among the methods belonging to the window object, there are some designed specifically to help your page communicate with the user by assisting with the input and output of information. The term modal means that script execution pauses, and all user interaction with the page is suspended, until the user clears the dialog.
The alert method takes a message string as its argument: The confirm dialog, though, provides the user with a. Clicking on either button clears the dialog and allows the calling script to continue, but the confirm method returns a different value depending on which button was clicked—Boolean true in the case of OK, or false in the case of Cancel.
Note that here, though, we pass the returned value of true or false to a variable so we can later test its value and have our script take appropriate action depending on the result. In this case, though, the dialog invites the user to enter information. A prompt dialog is called in just the same manner as confirm: The prompt method also allows for an optional second argument, giving a default response in case the user clicks OK without typing anything: If the user clicks OK or presses Enter without typing anything into the prompt dialog.
The prompt dialog generated by the previous code snippet is shown in Figure 4. Imagine your HTML contains the following element: Content of DIV element. The variable myDivContents will now contain the string value: Click here to view code image document.
We now have access to the chosen page element and all of its properties and methods. Caution Of course. Its methods enable you to use the list.
Click here to view code image history. We can think of the page URL as a series of parts: This can be an integer. You can use this property to find how many pages the user has visited: Computer Science. Electronic Engineering. Linux and Unix. Microsoft and. Mobile Computing. Networking and Communications.