Tech Time
JavaScript is fun! JavaScript is a simple-to-use scripting language that lets web page designers add interactive elements to their pages. Some of the participants at the Four Directions Summer Institute 1999 learned a little bit about web scripting, including JavaScript ,under the careful tutelage of Bart and Victor this past summer. If you have used the web moderately in the past year or two you probably have been exposed to JavaScript without even knowing it! JavaScript was created by Netscape and was active in Netscape 2.0. Internet Explorer does process JavaScript but not always in the same way as Netscape, so you do have to test your JavaScripted code with both browsers to ensure compatibility!
To understand a bit more about JavaScript, and other scripting languages, such as Perl, one has to know the difference between compiled and interpreted code. The major difference is that compiled code is turned into machine-readable code by the programmer with a program called a compiler. This is done only one time and thereafter the program loads and runs in machine code. Interpreted code, on the other hand, is turned into machine-readable code each and every time the code is used. As with all things there are advantages and disadvantages to each system. Compiled code generally runs faster because the machine that runs it does not have to translate it each time it is used. However interpreted code usually is easier for the novice to experiment with and change.
There are many good books on the subject and many more great web sites. If you are interested in this subject, do a web search and sample a few of the great tutorials and how-to-do-it pages you will find. As with HTML, a great way to learn JavaScript is to find a page that uses it, and deconstruct the code and then make changes to it to try to learn how it works!
Below is a sample of JavaScript code that I wrote recently. This script will generate a random number and then show a different picture each time the page is loaded. Many of you will recognize this, as I sent you the URL when I got it done. You will notice that the script is embedded in the head of the HTML page and a portion of it resides in the body. Feel free to copy this to a file and try it out!. This script requires that you have 5 JPG pictures in the same directory or folder as the page. Here are step-by-step instructions you can follow to make this work:
1. create a folder or directory called scrpttst
2. copy the HTML page below and paste it into a text editor and save it in your scrpttst directory as a text file with the name ranpics.htm
3. copy five JPG pictures into the same directory and name them pic1.jpg, pic2.jpg, pic3.jpg, pic4.jpg, and pic5.jpg
4. use Netscape or Internet Explorer to open the local page scrpttest/ranpics.htm
5. reload to see a different picture
If you do try this, you will notice that with only five pictures you get repeats quite often. This is just the nature of random numbers in small samples!
<html>
<head>
<script language="JavaScript">
<!-- Hide this script from old browsers --
var random = Math.random() ;
var picnum = Math.round(random*4)+1;
// -- End Hiding Here -->
</script>
</head>
<body BGCOLOR="#FFFFFF">
<center>
<font size=+Random Pictures!</FONT>
<br>
Hit Reload to see another picture!
<br>
<script language="JavaScript">
<!-- Hide this script from old browsers --
document.write('<IMG SRC=pic'+picnum+'.jpg>')
document.write('<br>')
document.write('picture number = '+picnum)
// -- End Hiding Here -->
</script>
<br>
Hit Reload to see another picture!
</center>
</body>
</html>
|