Javascript
lesson 1
<script language = "javascript">
<!--
//hides script from non supporting browsers
javascript program.................
..................
-->
// stops hiding the code to other browsers
</script>
See below for the code used on this web
page.
If this browser supports javascript
and SRC commands, a separate .js file will be included on the next line
declaring the fact. Javascript translator in earlier
versions of Netscape did not support SRC links.
<SCRIPT language="javascript">
<!-- hide from other browsers
//outcome if it works
document.writeln("The browser does support JavaScript!!!!!
So if no SRC comment is displayed above this line, then check the code
in the .js file for syntax errors.");
//stop hiding from other browsers -->
</SCRIPT>
</FONT>
Here's the result .............................
If no confirmation has been given up to now then your
browser does not support Java and JavaScript
Top of page
Non embedded JavaScript (SRC
Script attribute for accessing remote URL's) (Top
of page)
JavaScript does not always have to be embedded in the
HTML code. At the bottom of this article is a
lookup table written in Java and contained in a separate .js file so
that it can be accessed from any web page by inserting a pointer into the
HTML code on each page that needs to access the JavaScript program.
The source code for the following table is contained in a separate file named lookuplist.js The source code to produce the table comprises on one to two hundred lines, which would make the coding of each web page that used it rather lengthy, and the repetitative code would be difficult to maintain and modify in the future. However a single line of code on each web page using the JavaScript program is all that is required:-
<Script language = "Javascript" SRC = "lookuplist.js">
</Script>
It is now possible to maintain and modify a program which
can be used by many pages at one source. The lookup
list example contains 'href' links. It is similar in operation
to help files in application software. As the letters for the request
are typed into the input box the cursor jumps down to the nearest match.
The program has a weakness in that although the cursor moves, the window
does not scroll down with the cursor. The user needs to use the scroll
bar control.
The basic command unit is a one-line command or expression followed by a semi-colon eg.
document.writeln("This browser supports JavaScript");
The command causes the writeln() method to
occur, which is part of the document object. The semi-colon indicates
the end of the command.
Command blocks are used to group a set of JavaScript commands using {and} eg.
{
document.writeln("Not all browsers support JavaScript");
document.writeln("However this browser does support JavaScript
else you would not be reading this text");
}
Command blocks can be nested and it is good practice, as in other programs, to indent the the different levels to make the code more readable.
NOTE: In JavaScript, object, property, method, keyword, operator and variable names are all case sensitive.
Text can be output in a number of ways:
Comment statements can be inserted within the code using:
// for single line comments
or
/*......................
..................................
....................*/ for multiple
line statements.
ESCAPE CHARACTERS (HTML's PRE tag for preformatted text layout) (Go to top)
Characters that cannot be typed eg.
| \n new line | document.write("Sue\n","Delilah\n","Cahill\n") |
| \t tab | document.write("Mon\t","Tues\t","Wed\t") |
| \r carriage return |
OUTPUTTING GIF IMAGES (Go to top)
Gif images can also be ouput using the document.write method:-
<body>
<script language = "javascript">
<!-- Hide from other browsers
document.write('<IMG SCR="greenman.gif">');
So that's what the greenguy looks like?";
//stop hiding from other browsers -->
</script>
</body>
NOTE: that single quotes are used so that the nested IMG
SCR could use the required double quotes.
JavaScript allows programmers to generate output using dialogue boxes. The simplest way is to use the alert() method. (the alert() method is part of the window object and therefore doesn't have an object name in front of it. The alert box includes the phrase "JavaScript Alert" to distinguish it from dialogue boxes produced by other script). The following code displays a dialogue box before outputting the code, however used in this form it can become very annoying to the user, remember the opening dialogue box? that was generated by the following code.
<body>
<script language="javascript">
<!-- hide from other browsers
alert ("Welcome to the next piece of code");
document.write('<IMG SCR="spike.gif" >');
// stop hiding from other browsers -->
</script>
</body>
Promt() method creates a dialogue box which requires two pieces of data. The first is the text to be displayed and the second is the default data in the entry field.
An argument is the information contained in parenthesis attached to a method or function. Multiple arguments are separated by commas. The data returned by a method can be stored as a variable or the output strings can be concatonated.
<body>
<script language="javascript">
<!-- hide from other browsers
document.write('<IMG.SCR="spike.gif" >');
document.write("<H4> Hello, " + prompt("What
is your name?: ","Name") + ". I hope that you found this JavaScript lesson
and examples interesting </H4>");
//stop hiding from other browsers -->
</script>
</body>
|
|
|
|