Archive for September, 2007

1108 Part IV . JavaScript Core Language Reference (Com web hosting)

Tuesday, September 25th, 2007

1108 Part IV . JavaScript Core Language Reference Listing 41-4 (continued) function factorial(n) { if (n > 0) { return n * (factorial(n - 1)) } else { return 1 } }

Enter an input value:

Results:

This function is designed to be generalizable, accepting only the input value (n) as a parameter. In the form, the onClickevent handler of the button sends only the input value from one of the form s fields to the factorial() function. The returned value is assigned to the output field of the form. The factorial() function is totally ignorant about forms, fields, or buttons in this document. If I need this func tion in another script, I can copy and paste it into that script knowing that it has been pretested. Any generalizable function is part of my personal library of scripts from which I can borrow and saves me time in future scripting tasks. You cannot always generalize a function. Somewhere along the line in your scripts, you must have references to JavaScript or custom objects. But if you find that you re frequently writing functions that perform the same kind of actions, see how you can generalize the code and put the results in your library of ready-made functions. And if your audience uses browsers from Navigator 3 onward (and later versions of Internet Explorer 3 onward), consider placing these library functions in an external .jslibrary file. See Chapter 13 for details on this convenient way to share utility functions among many documents. Custom Objects In all the previous chapters of this book, you ve seen how conveniently the browser document object models organize all the information about the browser window and its document. What may not be obvious from the scripting you ve done so far is that JavaScript enables you to create your own objects in memory objects with properties and methods. These objects are not user-interface elements on the page but rather the kinds of objects that may contain data and script func tions (behaving as methods) whose results the user can see displayed in the browser window.
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.

1104 Part IV . JavaScript Core Language Reference (Web site development)

Wednesday, September 19th, 2007

1104 Part IV . JavaScript Core Language Reference Listing 41-2: Variable Scope Workbench Page Variable Scope Trials In this page, you define a number of variables some global, others local that are spread out in the document s Head and Body sections. When you load this page, it runs the testValues() function, which accounts for the current values of all the variable names. The script then follows up with one more value extraction that was masked in the function. The results of the page look like this: headGlobal is: Gumby headLocal value returned from head function is: Pokey aBoy is: Charlie Brown local version of hisDog is: Gromit global version of hisDog is intact: Snoopy
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.

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

Tuesday, September 18th, 2007

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.

1102 Part IV . JavaScript Core Language Reference (Http web server)

Tuesday, September 18th, 2007

1102 Part IV . JavaScript Core Language Reference As for succeeding parameters, the apply() method s second parameter is an array of values to be passed as parameters to the current function. The order of the values must match the order of parameter variables defined for the function. The call() method, on the other hand, enables you to pass individual parameters in a comma-delimited list. Your choice depends on how the parameters are carried along in your script. If they re already in array form, then use the apply()method; otherwise, use the call()method. The (ECMA) recommended way to invoke a function through this mechanism when no parameters need to be passed is via the call() method. toString() valueOf() Returns: String. NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5 Compatibility . . . Scripts rarely, if ever, summon the toString()and valueOf() methods of a function object. They work internally to allow debugging scripts to display a string version of the function definition. For example, when you enter the name of a func tion defined in The Evaluator into the top text box, JavaScript automatically con verts the function to a string so that its value can be displayed in the Results box. Using these methods or parsing the text they return has little, if any, practical application. Function Application Notes Understanding the ins and outs of JavaScript functions is the key to successful scripting, especially for complex applications. Additional topics covered in this chapter include the ways to invoke functions, variable scope in and around func tions, recursion, and the design of reusable functions. Invoking Functions A function doesn t perform any work until a script calls it by name or reference. Scripts invoke functions (that is, get functions to do something) via four routes: document object event handlers; JavaScript statements; HREF attributes pointing to a javascript:URL; and the more recent call() and apply()methods of function objects. The one approach not discussed at length yet in this book is the javascript: URL (some say pseudo-URL). Several HTML tags have HREF attributes that normally point to Internet URLs for navigating to another page or loading a MIME file that requires a helper application or plug-in. These HTML tags are usually tags for clickable objects, such as links and client-side image map areas. functionObject.toString()
Please visit Domain Name Hosting services for high quality webhost to host and run your jsp applications.

Web hosting provider - 1101Chapter 41 .Functions and Custom Objects function if

Monday, September 17th, 2007

1101Chapter 41 .Functions and Custom Objects function if your script has only a reference to the function. For example, if your script defines a function via the new Function() constructor (or other anonymous shortcut supported by the browser), you receive a reference to the function as a result of the constructor. To invoke the function later using only that reference (presumably preserved in a global variable), use either the apply()or call() method. Both of these methods achieve the same result, but choosing one method over the other depends on the form in which the function s parameters are conveyed (more about that in a moment). The first parameter of both methods is a reference to the object that the function treats as the current object. For garden-variety functions defined in your script, use the keyword this, which means that the function s context becomes the current object (just like a regular function). In fact, if there are no parameters to be sent to the function, you can omit parameters to both methods altogether. The object reference comes into play when the function being invoked is one that is normally defined as a method to a custom object. (I cover some of these concepts later in this chapter, so you may need to return here after you are familiar with custom objects.) Consider the following code that generates a custom object and assigns a method to the object to display an alert about properties of the object: // function to be invoked as a method from a car object function showCar() { alert(this.make + : + this.color) } // car object constructor function function car(make, color) { this.make = make this.color = color this.show = showCar } // create instance of a car object var myCar = new car( Ford , blue ) The normal way of getting the myCar object to display an alert about its properties is: myCar.show() At that point, the showCar() function runs, picking up the current car object as the context for the thisreferences in the function. In other words, when the showCar() function runs as a method of the object, the function treats the object as the current object. With the call()or apply()methods, however, you don t have to bind the showCar() function to the myCar object. You can omit the statement in the car() constructor that assigns the showCar function to a method name for the object. Instead, a script can invoke the showCar() method and instruct it to treat myCaras the current object: showCar.call(myCar) The showCar() function operates just as before, and the object reference in the call() method s first parameter slot is treated as the current object for the showCar() function. functionObject.apply()
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.

Web hosting reviews - 1100 Part IV . JavaScript Core Language Reference

Monday, September 17th, 2007

1100 Part IV . JavaScript Core Language Reference constructor See string.constructor (Chapter 34). length Value: Integer Read-Only Compatibility NN2 NN3 NN4 . NN6 . IE3/J1 IE3/J2 . IE4 . IE5 . IE5.5 . As the argumentsproperty of a function proves, JavaScript is very forgiving about matching the number of parameters passed to a function with the number of parameter variables defined for the function. But a script can examine the length property of a function object to see precisely how many parameter variables are defined for a function. A reference to the property starts with the function name rep resenting the object. For example, consider the following function definition shell: function identify(name, rank, serialNum) { … } A script statement anywhere outside of the function can read the number of parameters with the reference: identify.length The value of the property in the preceding example is 3. The lengthproperty supercedes the NN-only arityproperty. prototype See Array.prototype (Chapter 37). Methods apply([thisObj[, argumentsArray]]) call([thisObj[, arg1[, arg2[,…argN]]]]) Returns: Nothing. NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5 Compatibility . The apply() and call()methods of a function object invoke the function. This may seem redundant to the normal way in which script statements invoke functions by simply naming the function, following it with parentheses, passing parameters, and so on. The difference with these methods is that you can invoke the functionObject.apply()
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.

1099Chapter 41 .Functions and Custom Objects function gretel(x,y,z) (Hosting your own web site)

Sunday, September 16th, 2007

1099Chapter 41 .Functions and Custom Objects function gretel(x,y,z) { today = new Date() thisYear = today.getFullYear() hansel(x,y,z,thisYear) } When you load this page, the following results appear in the browser window (although the callerproperty values show undefinedfor NN6): hansel.caller is null hansel.arguments.length is 3 argument 0 is 1 argument 1 is two argument 2 is 3 hansel.caller is function gretel(x, y, z) { today = new Date(); thisYear = today.getFullYear(); hansel(x, y, z, thisYear); } hansel.arguments.length is 4 argument 0 is 4 argument 1 is five argument 2 is 6 argument 3 is 2001 (or whatever the current year is) As the document loads, the hansel()function is called directly in the body script. It passes three arguments, even though the hansel()function defines only two. The hansel.argumentsproperty picks up all three arguments just the same. The main body script then invokes the gretel() function, which, in turn, calls hansel() again. But when gretel() makes the call, it passes four parameters. The gretel() function picks up only three of the four arguments sent by the calling statement. It also inserts another value from its own calculations as an extra parameter to be sent to hansel(). The hansel.caller property reveals the entire content of the gretel()function, whereas hansel.arguments picks up all four parameters, including the year value introduced by the gretel() function. Neither the callernor argumentsproperties of a function object appear in the ECMA-262 Edition 3 specification. While NN6 dropped the callerproperty, it continues to support the arguments property probably because a lot of scripters use it. functionObject.caller
Please visit Domain Name Hosting services for high quality webhost to host and run your jsp applications.

1098 Part (Web server) IV . JavaScript Core Language Reference

Sunday, September 16th, 2007

1098 Part IV . JavaScript Core Language Reference caller Value: Function Object Reference Read-Only Compatibility NN2 NN3 . NN4 . NN6 IE3/J1 IE3/J2 . IE4 . IE5 . IE5.5 . Note The caller property, not part of the ECMA-262 standard, was removed from NN for version 6. When one function invokes another, a chain is established between the two primarily so that a returned value knows where to go. Therefore, a function invoked by another maintains a reference to the function that called it. Such information is automatically stored in a function object as the callerproperty. This relationship reminds me a bit of a subwindow s opener property, which points to the window or frame responsible for the subwindow s creation. The value is valid only while the called function is running at the request of another function; when a function isn t running, its caller property is null. The value of the callerproperty is a reference to a function object, so you can inspect its arguments and callerproperties (in case it was called by yet another function). Thus, a function can look back at a calling function to see what values it was passed. The functionName.caller property reveals the contents of an entire function definition if the current function was called from another function (including an event handler). If the call for a function comes from a regular JavaScript statement not originating from inside a function, the functionName.callerproperty is null. To help you grasp all that these two properties yield, study Listing 41-1. Listing 41-1: A Function s arguments and caller Properties