home *** CD-ROM | disk | FTP | other *** search
Wrap
Using JavaScript in HTML ------------------------------------------------------------------------ Embedding JavaScript in documents A script is embedded in HTML within a SCRIPT tag. <SCRIPT>...</SCRIPT> The text of a script is inserted between SCRIPT and its end tag. Attributes within the SCRIPT tag can be specified as follows: <SCRIPT LANGUAGE="JavaScript">...</SCRIPT> The LANGUAGE attribute is mandatory unless the SRC attribute is present and specifies the scripting language. <SCRIPT SRC="http://myscript.js">...</SCRIPT> The SRC attribute is optional and, if given, specifies a URL that loads the text of a script. NOTE: Not yet implemented for Beta4 release. <SCRIPT LANGUAGE="language" SRC=url>...</SCRIPT> Both attributes may be present. NOTE: Not yet implemented for Beta4 release. ------------------------------------------------------------------------ Usage Notes ΓÇóScripts placed within SCRIPT tags are evaluated after the page loads. Functions are stored, but not executed. Functions are executed by events in the page. ΓÇóSRC URL information is read in and evaluated as script container content. SRC script is evaluated before in-page script. ΓÇóThe SRC URL should use the .js suffix. ΓÇóA named SCRIPT tag may contain a function body that can be called in an onChange or other event-handler attribute. ΓÇóScripts may be placed inside comment fields to ensure that the script is not displayed when the page's HTML is viewed with a browser unaware of the SCRIPT tag. The entire script is encased by HTML comment tags: <!-- Begin to hide script contents from old browsers. // End the hiding here. --> ΓÇóLike Java, JavaScript is case-sensitive. ΓÇóUse single quotes (') to delimit string literals so that scripts can be distinguished from attribute values enclosed in double quotes. Example: <INPUT TYPE="button" VALUE="Press Me" onClick="myfunc('astring')"> ------------------------------------------------------------------------ Some Introductory Examples Example 1: A Simple Script. <HTML> <HEAD> <SCRIPT LANGUAGE="JavaScript"> document.write("Hello net.") </SCRIPT> </HEAD> <BODY> That's all, folks. </BODY> </HTML> Example 1 page display. Hello net. That's all folks. ------------------------------------------------------------------------ Example 2, a script with a function and comments. <HTML> <HEAD> <SCRIPT LANGUAGE="JavaScript"> <!-- to hide script contents from old browsers function square(i) { document.write("The call passed ", i ," to the function.","<BR>") return i * i } document.write("The function returned ",square(5),".") // end hiding contents from old browsers --> </SCRIPT> </HEAD> <BODY> <BR> All done. </BODY> </HTML> Example 2 page display. We passed 5 to the function. The function returned 25. All done. ------------------------------------------------------------------------ Example 3, a script with a form and an event handler attribute. <HTML> <HEAD> <SCRIPT LANGUAGE="JavaScript"> function compute(form) { if (confirm("Are you sure?")) form.result.value = eval(form.expr.value) else alert("Please come back again.") } </SCRIPT> </HEAD> <BODY> <FORM> Enter an expression: <INPUT TYPE="text" NAME="expr" SIZE=15 > <INPUT TYPE="button" VALUE="Calculate" ONCLICK="compute(this.form)"> <BR> Result: <INPUT TYPE="text" NAME="result" SIZE=15 > <BR> </FORM> </BODY> </HTML> Example 3 page display. Enter an expression: 9 + 5 Result: 14 ------------------------------------------------------------------------ Example 4, a script with a form and event handler attribute within a BODY tag. <HTML> <HEAD> <SCRIPT LANGUAGE="JavaScript"> function checkNum(str, min, max) { if (str == "") { alert("Enter a number in the field, please.") return false } for (var i = 0; i < str.length; i++) { var ch = str.substring(i, i + 1) if (ch < "0" || ch > "9") { alert("Try a number, please.") return false } } var num = 0 + str if (num < min || num > max) { alert("Try a number from 1 to 10.") return false } return true } function thanks() { alert("Thanks for your input.") } </SCRIPT> </HEAD> <BODY> <FORM> Please enter a small number: <INPUT NAME="num" ONCHANGE="if (!checkNum(this.value, 1, 10)) {this.focus();this.select();} else {thanks()}" VALUE="0"> </FORM> <SCRIPT LANGUAGE="JavaScript"> document.write("<PRE>") document.writeln("Field name: " + document.forms[0].num.name) document.writeln("Field value: " + document.forms[0].num.value) document.write("</PRE>") </SCRIPT> </BODY> </HTML> Example 4 page display. Please enter a small number: 7 Field name: num Field value: 7 ------------------------------------------------------------------------