|Language:||English, Spanish, Hindi|
|Genre:||Business & Career|
|ePub File Size:||21.33 MB|
|PDF File Size:||12.39 MB|
|Distribution:||Free* [*Regsitration Required]|
Click here to view code image myDiv. When using this within an event handler added via an attribute of an HTML element. However, support of ePUB and its many features varies across reading devices and applications. If you put a string into your variable and later want to interpret that value as a number. The keyword with takes an object as an argument. In the preceding example, the function volume a,b,c cannot be called from any place outside the constructor function as it has not been assigned to an object method by using this.
All Categories. Recent Books. IT Research Library. Miscellaneous Books. Someone who has never done any computer programming. Browse through an HTML primer before using this book. Stay ahead with the world's most comprehensive technology and business learning platform. With Safari, you learn the way you learn best.
In fact. Be careful—property names are case sensitive. The window object is at the top of the DOM tree. The window object Answers 1. Note the capital M. The program code is written in plain text.
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.
Having variable names such as productName and netPrice makes code much easier to read and maintain than if the same variables were called var and myothervar Alternatively we can combine these two statements conveniently and readably into one: We could then.
The following two lines of code are equivalent: To calculate the average correctly. So are these two: If you have doubts about the precedence rules.
When the user clicks on the button. This convention has arisen because. Replacing onmouseover with onmouseout in the code will. The onMouseOut event. When using this within an event handler added via an attribute of an HTML element. 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. Save the HTML file and open it in your browser. Summary You covered quite a lot of ground this hour.
None b. Exactly one c. Any number 3. Which of these is NOT a true statement about variables? Their names are case sensitive. They can contain numeric or non-numeric information.
You can access the image title using this. Can you think of an easy way to test whether your script has correctly set the new image title? Hour 3. How to define functions How to call execute functions How functions receive data Returning values from functions About the scope of variables Commonly, programs carry out the same or similar tasks repeatedly during the course of their execution.
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.
In this hour you see how to create and use functions.
Instead, it waits quietly until the function is called. To call a function, you simply use the function name with the parentheses wherever you want to execute the statements contained in the function: For example, you may want to add a call to your new function sayHello to the onClick event of a button: Tip Function names, like variable names, are case-sensitive. A function called MyFunc is different from another called myFunc.
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. Caution Make sure that your function calls contain enough argument values to match the arguments specified in the function definition.
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.
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. A message about the current value of the other, global variable a is then appended to the message, and the message displayed to the user.
You learned how to call functions from within your code, and pass information to those functions in the form of arguments.
You also found out how to return information from a function to its calling statement. 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 return value from a prompt dialog depends on what option the user takes: If the user types in input and clicks OK or presses Enter. If the user dismisses the dialog that is. When treated as a number it takes the value 0.
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: There is also the method go.
The properties are listed in Table 4. The only property owned by the history object is its length. The history object has three methods. To avoid this and get the page directly from the server.
Click here to view code image location. TABLE 4. If you would rather the sending page were removed from the history list and replaced with the new URL. This replaces the old URL with the new one both in the browser and in the history list. Use your editor to create the file navigator.
Save the file and open it in your browser. Figure 4. Why is it reporting the appName property as Netscape. The result is that the object provides.
Not all properties are supported in all browsers hence the failure to report the cpuClass property in the preceding example.
Each Date object we create can represent a different date and time. Querying the navigator object is nearly always the wrong way to do it. Note Later in the book we talk about feature detection. There is no Date object already created for you as part of the DOM. Create a Date Object with the Current Date and Time This is the simplest way to create a new Date object containing information about the. Although cross-browser standards compliance is closer than it was a few years ago.
Dates and Times The Date object is used to work with dates and times. There are several ways to do this: The variable mydate is an object containing information about the date and time at the moment the object was created.
The preceding example calculates a date 33 days in the future. We can also carry out date and time arithmetic.
A full list of the methods of the Date object is available in Appendix B. A full list of the available methods is available in Appendix B. Click here to view code image Object created on day number 5 After amending date to 15th.
Unlike the Date object. Note the use of toDateString and toTimeString. Finding Minimum and Maximum We can use min and max to pick the largest and smallest number from a list: If the fractional part is less than. The output as written to the page looks like this: Rounding The methods ceil. As Math. In other words. The youngest person is 19 years old The oldest person is 31 years old Random Numbers To generate a random number.
The statements within that code block can call methods without specifying an object. Reading the Date and Time We put into practice some of what we covered in this hour by creating a script to get the current date and time when the page is loaded. We also implement a. By using with you can save yourself some tedious typing.
Take a look at Listing 4. As we do so. As you will recall. We can access the individual parts of the time and date using getDate.
In addition to the get.