Working with JavaScript
5 ways to integrate
JavaScript into HTML documents:
Placement |
Reasons |
In a <script> tag within the <head> of an HTML document |
To declare user-defined functions |
In a <script> tag within the <body> of an HTML document |
To produce content for inclusion in an HTML document |
Inline with HTML as an event handler |
To respond to a particular event, e.g., onLoad, onClick, onSubmit |
In an external JavaScript file: as a text file with .js extension |
To declare functions that are used again and again with a variety of HTML documents |
Inline using the JavaScript pseudo-protocol: in the href attribute of an anchor (<a></a>) or area tag |
To execute one or more JavaScript statements instead of going to a document or resource |
Debugging:
Typing javascript: into the location bar in Netscape Navigator will cause the JavaScript Console to pop up
Hiding scripts from
old browsers:
<script
language=”JavaScript”>
<! --
…
-->
</script>
Commenting in JavaScript:
JavaScript statement // comment
/* comment line 1
…
/*
Using semicolon as
statement terminator:
A must if you place more than one statement on a single line
JavaScript is case
sensitive:
Follow the spelling of object and method, e.g., document.write is correct whereas document.Write is not.