Javascript lesson 1
 
 CONTENTS
Hiding javascript from non supporting browsers Lookup list program, example of .js separate file
Using external files for JavaScript programs Example of interactive code
SRC links for JavaScript programs in separate files Example of greenguy.gif outputs 
Basic Command Syntax Interacting with the user
Comments within code (rem statements) Using dialogue boxes
Outputting text with ESC Outputting GIFs with text


Does the Browser support JavaScript?  ( top of page )
If a browser doesn't support JavaScript then it would not know how to deal with the code between the  <script> </script>tags.  It is therefore necessary to include instructions telling it to ignore the code as follows:-:

<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.



Basic command Syntax (document.write() and document.writeln() methods) (Go to top)

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 (Go to top)

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:

The text usually takes on the text enhancement currently being applied in the HTML code that it is embedded in.


COMMENTS (Go to top)

Comment statements can be inserted within the code using:

// for single line comments
or
/*......................
..................................
....................*/    for multiple line statements.



OUTPUT DATA

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
 
These handle the layout differently when enclosed within the preformatted,  <pre> and </pre>, tags as in the following demonstration
  




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.




 

 WORKING WITH DIALOGUE BOXES (alert() method message dialogue box) (Go to top)

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>



You have obviously answered "OK" to the dialogue box to be viewing this rotating spike.  If you would like to try again right-click your mouse button and select reload.
 


INTERACTING WITH THE USER (prompt() method interactive dialogue box) (Go to top)

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>



Lookuplist.js table this table has been written in JavaScript and is saved in another file on this web site.  It can be accessed by any web page that includes <SCRIPT="javascript" SRC="lookuplist.js"></SCRIPT> comments in it's HTML code.
 
 Return to SCR links topic?



This comment is the result when you replied to the dialogue box requesting your name as you entered this lesson.  If you would like to see it again right-click your mouse button and select reload.
End of lesson 1  Go to top 

 
 Top of Page 
Home Page 
Query for Tutor?