Java Reference
In-Depth Information
alert("You must enter a term.");
else
form.submit();
}
</script>
As you can see, the format for JavaScript is slightly different than Java. For example, the
function declaration starts with the keyword function . Additionally, none of the variables
have a type declaration.
You will find this <script> declaration mixed in with regular HTML on web sites.
Some older browsers can not properly process JavaScript. These older browsers will simply
display the JavaScript code to the end user. To prevent this from happening, HTML com-
ments were often inserted around the JavaScript code.
Though every major browser supports JavaScript, you will still often see JavaScript code
enclosed in HTML comments. For example, the above function could also be expressed as
follows, using HTML comments:
<script type="text/javascript">
<!--
function formValidate(form){
if( form.interest.value.length==0 )
alert("You must enter an interest rate.");
else if( form.principle.value.length==0 )
alert("You must enter a principle.");
else if( form.term.value.length==0 )
alert("You must enter a term.");
else
form.submit();
}
//-->
</script>
In addition to directly inserting JavaScript code into HTML, JavaScript also supports an
“include”. Using a JavaScript include statement allows JavaScript, from an external file, to be
included in the HTML document. This allows you to group commonly used functions into an
“include file” that can be accessed from many different HTML documents. The following line
of code demonstrates a JavaScript include:
<script type="text/javascript" src="include.js"></script>
Include files normally end with a .js file extension.
Search WWH ::




Custom Search