home *** CD-ROM | disk | FTP | other *** search
/ Java Interactive Reference Guide / Java Interactive Reference Guide.iso / autorun / java.exe / java.DXR / 00112_8.txt < prev    next >
Encoding:
Text File  |  1980-01-03  |  7.9 KB  |  233 lines

  1. Authoring with JavaScript
  2.  
  3.  
  4.  
  5. ΓÇóUsing JavaScript in HTML ΓÇóScripting Event Handlers ΓÇóTips and Techniques 
  6.  
  7.  
  8.  
  9. Using JavaScript in HTML 
  10.  
  11.  
  12.  
  13. There are two ways to embed JavaScript in an HTML document: 
  14.  
  15. ΓÇówith the SCRIPT tag. This is the way you define functions and perform statements within a page. ΓÇóas event handlers in HTML tags. Scripting event handlers is discussed in the next section. 
  16.  
  17.  
  18.  
  19. SCRIPT is an HTML tag that enables you to include any JavaScript statements in a document. The tag, <SCRIPT>, and its closing counterpart, </SCRIPT> may enclose any number of JavaScript statements. It is a good idea to enclose the SCRIPT tag in HTML comments to hide the script from old browsers that do not recognize JavaScript. That way, your JavaScript code won't appear as a bunch of unformatted gobbledygook to users with old browsers. 
  20.  
  21. It is important to understand the difference between defining a function and calling the function. Defining the function simply names the function and specifies what to do when the function is called. Calling the function actually performs the specified actions with the indicated parameters. 
  22.  
  23. Generally, it is good practice to define the functions for a page in the HEAD portion of a document. Since the HEAD is loaded first, this practice guarantees that the functions will be loaded before the user has a chance to do anything that might call a function. For example: 
  24.  
  25.  
  26. <HEAD>
  27. <!--- hide script from old browsers
  28. <SCRIPT>
  29. function bar() {
  30.    document.write("<HR>")
  31. }
  32. function output(head, level, string) {
  33.    document.write("<H" + level + ">" + head + "</H" + level + "><P>" + string)
  34. }
  35. // end hiding from old browsers -->
  36. </SCRIPT>
  37. </HEAD>
  38.  
  39.  
  40.  
  41.  
  42.  
  43. This part of a document HEAD defines two simple functions: bar, that displays an HTML horizontal rule, and output, that displays a string as an HTML paragraph with a heading of the specified level. 
  44.  
  45. Scripting Event Handlers 
  46.  
  47.  
  48.  
  49.  
  50.  
  51. JavaScript applications in the Navigator are largely event-driven. Events are actions that occur, usually as a result of something the user does. For example, a button click is an event, as is giving focus to a form element. There is a specific set of events that Navigator recognizes. You can define Event handlers, scripts that are automatically executed when an event occurs. 
  52.  
  53. Event handlers are embedded in documents as attributes of HTML tags to which you assign JavaScript code to execute. The general syntax is 
  54.  
  55.  
  56. <TAG eventHandler="JavaScript Code">
  57.  
  58.  
  59.  
  60. where TAG is some HTML tag and eventHandler is the name of the event handler. 
  61.  
  62. For example, suppose you have created a JavaScript function called compute. You can cause Navigator to perform this function when the user clicks on a button by assigning the function call to the button's onClick event handler: 
  63.  
  64.  
  65. <INPUT TYPE="button" VALUE="Calculate" onClick="compute(this.form)">
  66.  
  67.  
  68.  
  69.  
  70.  
  71. You can put any JavaScript statements inside the quotes following onClick. These statements get executed when the user clicks on the button. If you want to include more than one statement, separate statements with a semicolon (;). 
  72.  
  73. In general, it is a good idea to define functions for your event handlers because: 
  74.  
  75. ΓÇóit makes your code modular-you can use the same function as an event handler for many different items. ΓÇóit makes your code easier to read. 
  76.  
  77.  
  78.  
  79. Notice in this example the use of this.form to refer to the current form. The keyword this refers to the current object-in the above example, the button object. The construct this.form then refers to the form containing the button. In the above example, the onClick event handler is a call to the compute() function, with this.form, the current form, as the parameter to the function. 
  80.  
  81. Events apply to HTML tags as follows: 
  82.  
  83. ΓÇóFocus, Blur, Change events: text fields, textareas, and selections ΓÇóClick events: buttons, radio buttons, checkboxes, submit buttons, reset buttons, links ΓÇóSelect events: text fields, textareas ΓÇóMouseOver event: links 
  84.  
  85.  
  86.  
  87. If an event applies to an HTML tag, then you can define an event handler for it. In general, an event handler has the name of the event, preceded by "on." For example, the event handler for the Focus event is onFocus. 
  88.  
  89. Many objects also have methods that emulate events. For example, button has a click method that emulates the button being clicked. Note: The event-emulation methods do not trigger event-handlers. So, for example, the click method does not trigger an onClick event-handler. However, you can always call an event-handler directly (for example, you can call onClick explicitly in a script). 
  90.  
  91.  
  92. Event     Occurs when...     Event Handler 
  93. blur     User removes input focus from form element     onBlur 
  94. click     User clicks on form element or link     onClick 
  95. change     User changes value of text, textarea, or select element     onChange 
  96. focus     User gives form element input focus     onFocus 
  97. load     User loads the page in the Navigator     onLoad 
  98. mouseover     User moves mouse pointer over a link or anchor     onMouseOver 
  99. select     User selects form element's input field     onSelect 
  100. submit     User submits a form     onSubmit 
  101. unload     User exits the page     onUnload 
  102. enable     NYI     NYI 
  103. disable     NYI     NYI 
  104.  
  105.  
  106.  
  107.  
  108. Tips and Techniques 
  109.  
  110.  
  111.  
  112. This section describes various useful scripting techniques. 
  113.  
  114. Using Quotes
  115.  
  116.  
  117.  
  118. Be sure to alternate double quotes with single quotes. Since event handlers in HTML must be enclosed in quotes, you must use single quotes to delimit arguments. For example 
  119.  
  120.  
  121. <FORM NAME="myform">
  122. <INPUT TYPE="button" NAME="Button1" VALUE="Open Sesame!" 
  123. onClick="window.open('stmtsov.html', 'newWin', 'toolbar=no,directories=no')">
  124. </FORM>
  125.  
  126.  
  127.  
  128.  
  129.  
  130. Defining Functions
  131.  
  132.  
  133.  
  134. It is always a good idea to define all of your functions in the HEAD of your HTML page. This way, all functions will be defined before any content is displayed. Otherwise, the user might perform some action while the page is still loading that triggers an event handler and calls an undefined function, leading to an error. 
  135.  
  136. Creating Arrays
  137.  
  138.  
  139.  
  140. An array is an ordered set of values that you reference through an array name and an index. For example, you could have an array called emp, that contains employees' names indexed by their employee number. So emp[1] would be employee number one, emp[2] employee number two, and so on. 
  141.  
  142. JavaScript does not have an explicit array data type, but because of the intimate relationship between arrays and object properties (see JavaScript Object Model), it is easy to create arrays in JavaScript. You can define an array object type, as follows: 
  143.  
  144.  
  145. function MakeArray(n) {
  146.    this.length = n;
  147.    for (var i = 1; i 
  148.  
  149.  
  150.  
  151. This defines an array such that the first property, length, (with index of zero), 
  152. represents the number of elements in the array.  
  153. The remaining properties have an integer index of one or greater, and are initialized to zero.
  154.  
  155.  
  156.  
  157. You can then create an array by a call to new with the array name, 
  158. specifying the number of elements it has. For example:
  159.  
  160. emp = new MakeArray(20);
  161.  
  162.  
  163.  
  164.  
  165.  
  166. This creates an array called emp with 20 elements, and initializes the elements to zero.
  167.  
  168.  
  169.  
  170.  
  171.  
  172. Populating an Array
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180. You can populate an array by simply assigning values to its elements.  For example:
  181.  
  182. emp[1] = "Casey Jones"
  183. emp[2] = "Phil Lesh"
  184. emp[3] = "August West"
  185.  
  186.  
  187. and so on.
  188.  
  189.  
  190.  
  191.  
  192. You can also create arrays of objects.  For example, suppose you define an object type
  193. named Employees, as follows:
  194.  
  195.  
  196. function Employee(empno, name, dept) {
  197.    this.empno = empno;
  198.    this.name = name;
  199.    this.dept = dept;
  200. }
  201.  
  202.  
  203.  
  204.  
  205.  
  206. Then the following statements define an array of these objects:
  207.  
  208.  
  209. emp = new MakeArray(3)
  210. emp[1] = new Employee(1, "Casey Jones", "Engineering")
  211. emp[2] = new Employee(2, "Phil Lesh", "Music")
  212. emp[3] = new Employee(3, "August West", "Admin")
  213.  
  214.  
  215.  
  216.  
  217.  
  218. Then you can easily display the objects in this array using the show_props function
  219. (defined in the section on the JavaScript Object Model) as follows:
  220.  
  221. for (var n =1; n ");
  222. }
  223.  
  224.  
  225.  
  226.  
  227.  
  228. More information in this section TBD.
  229.  
  230.  
  231.  
  232.  
  233.