1103Chapter 41 .Functions and Custom Objects A JavaScript-enabled (Most popular web site)

1103Chapter 41 .Functions and Custom Objects A JavaScript-enabled browser has a special, built-in URL pseudo-protocol javascript: that lets the HREF attribute point to a JavaScript function or method rather than to a URL out on the Net. For example, it is common practice to use the javascript:URL to change the contents of two frames from a single link. Because the HREFattribute is designed to point to only a single URL, you d be out of luck without a convenient way to put multiframe navigation into your hands. Implement multiframe navigation by writing a function that sets the location.href properties of the two frames; then invoke that function from the HREFattribute. The following example shows what the script may look like: function loadPages() { parent.frames[1].location.href = page2.html parent.frames[2].location.href = instrux2.html } … Next Note These kinds of function invocations can include parameters, and the functions can do anything you want. One potential side effect to watch out for occurs when the function returns a value (perhaps the function is also invoked from other script locations where a returned value is expected). Because the HREFattribute sets the TARGET window to whatever the attribute evaluates to, the returned value is assigned to the TARGET window probably not what you want. To prevent the assignment of a returned value to the HREF attribute, prefix the function call with the voidoperator: If you don t want the HREFattribute to do anything (that is, let the onClick event handler do all the work), then assign a blank function after the operator: Experienced programmers of many other languages recognize this operator as a way of indicating that no values are returned from a function or procedure. The operator has that precise functionality here, but in a nontraditional location. Variable Scope: Globals and Locals A variable can have two scopes in JavaScript. As you might expect, any variable initialized within the main flow of a script (not inside a function) is a global variable in that any statement in the same document s script can access it by name. You can, however, also initialize variables inside a function (in a var statement) so the variable name applies only to statements inside that function. By limiting the scope of the variable to a single function, you can reuse the same variable name in multiple functions thereby enabling the variables to carry very different information in each function. Listing 41-2 demonstrates the various possibilities.
Please visit our
professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.

Leave a Reply