
Javascript Tutorial: Creating a Simple Script
Home > Build
> Programming > Javascript
> Javascript
Tutorial
by Boris
Mordkovich
THE <script> TAG
When defining scripts in the head and body of
HTML documents you are required to use a script tag, while
if you are defining a script within an event handler, this
tag is not required.
There are several attributes that you can use other than the
src attribute you learned about earlier. You can also include
the language attribute. This allows you to define the script
within the script tag as being Javascript. You can also define
the version of Javascript. You can put the type attribute,
it will always be text/javascript.
None of these are necessary but they will help with compatibility
for older browsers. If a browser does not support that version
of Javascript, instead of displaying errors, it will not run
the script. The below statement defines a Javascript with
the version of 1.3 and using the external menu.js
file.
<script
language="JavaScript1.3" src="menu.js" type="text/javascript">
JAVASCRIPT VERSIONS
- Javascript 1.0 - supported by Netscape 2.0 and Internet
Explorer 3.0
- Javascript 1.1 - supported by Netscape 3.0 and mostly
supported by Internet Explorer 4.0
- Javascript 1.2 - supported by Netscape 4.0 and partially
supported by Internet Explorer 4.0
- Javascript 1.3 - supported by Netscape 4.5
OLDER BROWSERS
To hide your scripts from older browsers you
can enclose them with comments (just the script, not the script
tag).
<script
language="Javascript">
<!--
your script here
// -->
</script>
You can also include the <noscript> tags to display
content for visitors with browsers that don't support Javascript.
JAVASCRIPT COMMENTS
You can use comments for the same things you
use them in HTML, to include notes to remind you of the purpose
of certain parts of the page. There are two ways to include
comments.
// this
would be a comment
Using the two forward slashes (//)
hides everything on that line. If you need to make more than
one line a comment you would use the following method.
/*
this would be a comment
this would also be a comment
*/
Everything after the /* and before
the */ becomes a comment.