HTML Script Embedding

This tutorial is not intended to teach script writing, but will only define some of the related tags and give an example script so the user can get an idea how to embed script in HTML. This tutorial also does not explain how to get information entered in a script form to the server. That is a subject that will be described when the hyper text transfer protol (HTTP) and server side script writing is discussed.

The script below will check inputs from the form, below, to be sure they have been entered correctly before sending the data to the server. If you attempt to press submit in the form below without filling the appropriate fields in, you will see that the script is active.


SCRIPT Element Attributes

The script element is usually embedded in the document header but may be placed in the head or the body of the HTML document so long as it occurs before code that invokes it.

  • CHARSET - (Depreciated). The encoding of any external script specified such as "iso-8859-1".
  • DEFER - (Depreciated). The script is not parsed until the HTML document is rendered.
  • EVENT - Specifies the event used to trigger the script.
  • FOR - Specifies an object (combined with an action) that is used to trigger the script event
  • LANGUAGE - (Depreciated). Tells the name of the language the script is written in. Required for most browsers. Possible values are JavaScript, VBScript, and JavaScript1.1
  • SRC - (Depreciated). Specifies an external script which may include a URL path to another webserver.
  • TYPE - The new method to specify the script language using MIME specification rather than the LANGUAGE attribute. This attribute is not widely supported yet.

The NOSCRIPT element is used to provide alternate content when a browser does not support script.


Scripting Events

  • ondblclick
  • onclick
  • onkeydown
  • onkeypress
  • onkeyup
  • onmousedown
  • onmousemove
  • onmouseout
  • onmouseover
  • onmouseup

An example Script

How the script looks:




<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<head>
<title>New CTDP Member Registration</title>
<scrypt type="text/javascript" for="cmdSubmit" event="onclick" language=JavaScript>
<!--

var MemSubForm;
MemSubForm = document.RegistrationForm;

if (MemSubForm.firstname.value.length == 0) {
   MemSubForm.firstname.value = prompt("Please enter your first name.");
	if (MemSubForm.firstname.value.length != 0)
	MemSubForm.lastname.focus();
}
if (MemSubForm.lastname.value.length == 0) {
	MemSubForm.lastname.value = prompt("Please enter your last name.");
	if (MemSubForm.lastname.value.length != 0)
	MemSubForm.addr1.focus();
}
if (MemSubForm.addr1.value.length == 0) {
	MemSubForm.addr1.value = prompt("Please enter your address.");
	if (MemSubForm.addr1.value.length != 0)
	MemSubForm.addr1.focus();
}
if (MemSubForm.city.value.length == 0) {
	MemSubForm.city.value = prompt("Please enter your city of residence.");
	if (MemSubForm.city.value.length != 0)
	MemSubForm.city.focus();
}
if (MemSubForm.state.value == 0) {
	alert("Please select a 2-letter state abbreviation.");
	MemSubForm.state.focus();
}
else if (MemSubForm.code.value.length != 3 ||
	isNaN(MemSubForm.code.value)) {
	alert("Please enter a valid telephone area code.");
	MemSubForm.code.focus();
	MemSubForm.code.select(	);
}
else if (MemSubForm.phone.value.length != 7 ||
	isNaN(MemSubForm.phone.value)) {
	alert("Please enter a valid phone number.");
	MemSubForm.phone.focus();
	MemSubForm.phone.select();
}
else if (MemSubForm.zip.value.length < 5) {
	MemSubForm.zip.value = prompt("Please enter a valid zip code.");
	if (MemSubForm.zip.value.length != 0)
	MemSubForm.zip.focus();
}
// -->
</script>
</head>
<body>

The Form HTML Code

<h2 style="text-align: center">Computer Technology Documentation Project</h2>
<h2 style="text-align: center">Member Registration</h2>
<hr size="5" width="90%">
 
<div style="text-align: center">
<table>
<td>
<pre>
<form name="RegistrationForm">
<b>First Name:   </b><input type="text" name="firstname">
<b>Last Name:   </b><input type="text" name="lastname">
<b>Address1:   </b><input type="text" name="addr1">
<b>Address2:   </b><input type="text" name="addr2">
<b>City:   </b><input type="text" name="city">
<b>State:   </b><select name="state" size="1">
	<option value="1">AL
	<option value="2">AK
	<option value="3">AR
	<option value="4">AS
	<option value="5">AZ
	<option value="6">CA
	<option value="7">CO
	<option value="8">CT
	<option value="9">DC
	<option value="10">DE
	<option value="11">FL
	<option value="12">FM
	<option value="13">GA
	<option value="14">GU
	<option value="15">HI
	<option value="16">ID
	<option value="17">IA
	<option value="18">IL
	<option value="19">IN
	<option value="20">KS
	<option value="21">KY
	<option value="22">LA
	<option value="23">MA
	<option value="24">MD
	<option value="25">ME
	<option value="26">MH
	<option value="26">MI
	<option value="28">MN
	<option value="29">MO
	<option value="30">MP
	<option value="31">MS
	<option value="32">MT
	<option value="33">NC
	<option value="34">ND
	<option value="35">NE
	<option value="36">NH
	<option value="37">NJ
	<option value="38">NM
	<option value="39">NV
	<option value="40">NY
	<option value="41">OH
	<option value="42">OK
	<option value="43">OR
	<option value="44">PA
	<option value="45">PR
	<option value="46">PW
	<option value="47">RI
	<option value="48">SC
	<option value="49">SD
	<option value="50">TN
	<option value="51">TX
	<option value="52">UT
	<option value="53">VA
	<option value="54">VI
	<option value="55">VT
	<option value="56">WA
	<option value="57">WI
	<option value="58">WV
	<option value="59">WY
</select>
<b>Zip:		</b><input type="text" name="zip" maxlength="10" size="10">
<b>Area Code/Phone:</b><input type="text" 
name="code" maxlength="3" size="3">
<input type="text" name="phone" maxlength="7" size="7">

<div style="text-align: center">
<input type="button" name="cmdSubmit" value="Submit"> <input type="reset"
 value="clear">
</div>
</pre>

</form>
</td>
</table>
</div>
</body>



How the Form Appears and Works:

New CTDP Member Registration

Computer Technology Documentation Project

Member Registration


First Name: Last Name: Address1: Address2: City: State: Zip: Area Code/Phone: