home -- outline -- lectures -- assignments -- discussion -- tips -- links -- | |
![]() ![]() |
|
![]() |
Monday March 10 The wonders of Javascript Ok, first of all, for anything and everything JavaScript, see: Netscape's JavaScript Guide. This entire lecture comes from the material contained in these pages (less the propaganda). What is Javascript? According to our friends at Netscape:
JavaScript Basics Despite the differences between client and server side JavaScript, there are some basic similarities which Netscape calls "the Core Language" (sounds so important!)
Some important things to note:
JavaScript on the Client Side For now, let's concentrate on what Javascript can do for you on the Client side.
Using JavaScript on your HTML pages
The < SCRIPT > tag
Functions in JavaScript We have been using Perl to write cgi, but we have never actually introduced the idea of a function. A function is a block of code that performs some operation. This block always has a name, so that it can be "called" (used) and often has arguments, so that it knows what to act upon when called. Why use functions? Because if you store some code in a function, you can just call it instead of writing it out all the time. Consider your Rolodex assignment, as well as the Virtual Museum. There were several times that you searched several files for a particular keyword. While the conditions of this search changed, the basic operation was ususally the same. When it seems like you are writing the same code over and over, it's time to start thinking about functions. So here is an example of a function (located in the HEAD of a html document) and a subsequent "call" in the BODY of the same document: <HEAD> <SCRIPT LANGUAGE="JavaScript"> <!-- Hide script from old browsers function square(number) { return number * number; } // End script hiding from old browsers --> </SCRIPT> </HEAD> <BODY> <SCRIPT> document.write("The function returned ", square(5), "."); </SCRIPT> <P> All done. </BODY> So how does this work?? The function square takes one argument, called number. The function consists of one statement return number * number that indicates to return the argument of the function multiplied by itself. The return statement specifies the value returned by the function. In the BODY of the document, the statement square(5) calls the function with an argument of five. The function executes its statements and returns the value twenty-five. The script displays the following results: The function returned 25. All done. The square function used the line document.write(...) to display output in Navigator, like this NOTE: Another thing we mentioned before were objects...for now, just think of objects as extra special functions. Dynamic Generation of HTML As we just saw, the write method of document displays output in the Navigator. Just like the print statement in Perl, the write method lets you display text conditionally or based on variable arguments. The write method takes any number of string arguments that can be string literals ("this is a literal string") or variables (like $var in Perl). Now, in Perl, we just separated variables with spaces in our print statements: print "$last $first"; In JavaScript you use the string concatenation operator (+) to create one string from several. Consider the following script, which generates dynamic HTML with Navigator JavaScript: <HEAD> <SCRIPT> <!--- Hide script from old browsers // This function displays a horizontal bar of specified width function bar(widthPct) { document.write("<HR ALIGN='left' WIDTH=" + widthPct + "%>"); } // This function displays a heading of specified level and some text function output(headLevel, headText, text) { document.write("<H", headLevel, ">", headText, "</H", headLevel, "><P>", text) } // end script hiding from old browsers --> </SCRIPT> </HEAD> <BODY> <SCRIPT> <!--- hide script from old browsers bar(25) output(2, "JavaScript Rules!", "Using JavaScript is easy...") // end script hiding from old browsers --> </SCRIPT> <P> This is some standard HTML, unlike the above that is generated. </BODY> How this works: The HEAD of this document defines two functions:
|