JavaScript

JavaScript Primer--http://www.wsabstract.com/javatutors/primer1.shtml

The JavaScript Source--http://javascript.internet.com/

Website Abstraction/JavaScript Tutorials--http://www.wsabstract.com/javaindex.shtml

What is JavaScript?


Where does your JavaScript code go?

What does JavaScript look like?

Here is a short sample of JavaScript within the HTML of a web page. It writes the words Hi there. This text is an example of text written using javascript! on a web page.

<HTML>

<HEAD><TITLE>JS</TITLE>

<SCRIPT LANGUAGE="JavaScript">
 

document.write("Hi there. This text is an example of text written using javascript!")


</SCRIPT>

</HEAD>

<BODY></BODY>

</HTML>

The web page is called a document. This is an example of an "object", the basis for all of JavaScript. Objects usually have properties and methods. Examples of document properties are bgcolor (written as document.bgcolor) and title (written as document.title). Windows, frames, and many other page elements are objects. You can assign values to properties (e.g. document.bgcolor="#FFFF00") using JavaScript. A method is something you do to an object. In the short example above, you are writing something in the document, so write is the method.

JavaScript functions are small programs that accomplish something. Each function must have a unique name. They are "called" from somewhere in the web page (a button, a click, another function, etc.). Functions often contain some kind of variable and other types of JavaScript commands (e.g., IF, FOR object.property.method statements, calculations, etc.). Braces ({}) are used throughout a function to show where the functions begins/ends and to group statements that belong together. Semicolons (;) are often used between lines. The function raiseP below takes in two numbers (shown as the variables x and y), then raises x to the power of y. For example, a line in the web page that says var Z=raiseP(4,3) results in the number 64 (4 3) being returned to the variable Z.
 
 
function raiseP(x,y) the name of the function and the two variables x & y
brace shows "begin here"
total=1; start with a total of 1
for (j=0; j<y; j++) a "loop", the calculation below is repeated y times,
variable j is used to keep count, j starts at 0 and
repeats as long as j is less than y, j++ means add 1 each loop
{ total*=x; } variable total is multiplied by x, the new amount replaces old 
total, braces show what is repeated
return total;  variable total (the answer) is sent back
}  brace shows "end here"

Remember: JavaScript is "case sensitive," so watch your capitalization.

The example below shows one of the examples on the class website. It makes a web page that continuously scrolls a message at the bottom of the window. All the JavaScript code is in the <HEAD> section, so it is run automatically when the page is loaded.
 
 
<HTML>  
<HEAD>  
<script language="JavaScript"> Javascript begins here
var scrollCounter=0;  variables begin here
var scrollText="Welcome to Room 1117's web page! Isn't it SPECIAL?"; the message is put into scrollText
var scrollDelay=70; other variables
var i=0;  are set up
while(i++<140)  another kind of loop, adds 140 spaces to message, it means 
"keep adding 1 to the variable i as long as i is less than 140"
scrollText=" " + scrollText;
add a space at the font of message
function Scroller()  here's the function definition for Scroller()
Notice the () after Scroller, all fucntions have them
{ braces show contents of Scroller()
window.status=scrollText.substring(scrollCounter++,scrollText.length)  put part of message at bottom of window
if (scrollCounter = = scrollText.length)  Are we done yet?
scrollCounter=0; If so, set the counter back to 0
setTimeout("Scroller()",scrollDelay);  wait just a little while, then do Scroller() again--just like recursion in Logo!
} end of Scroller() definition
Scroller(); actually start Scroller()
</script> script ends here
</HEAD>  
<BODY>  
<h1>Scroller Example</h1>  not much on the
<p>See bottom of screen for a short message.</p>  actual web page
</BODY>  
</HTML>  

Here's another example (writing the current date in a web page). Only the JavaScript and surrounding code is shown.
 
 
<HEAD>  
<TITLE>SHOW  DATE ON THE WEB PAGE</TITLE>  
<SCRIPT language="JavaScript">  
function write_date( ) define the write_date ( ) function
{  
var thetime=new Date( ); get the date/time from the computer
var nday=thetime.getDay( );  
var nmonth=thetime.getMonth( ); split it up into its component parts
var ntoday=thetime.getDate( ); and put it into variables
var nyear=thetime.getYear( );  
var AorP=" ";  
if (nday==0)  nday="Sunday";  
if (nday==1)  nday="Monday";  
if (nday==2)  nday="Tuesday"; determine the name of the current day, based on 
if (nday==3)  nday="Wednesday"; the number from the system
if (nday==4)  nday="Thursday";  
if (nday==5)  nday="Friday";  
if (nday==6)  nday="Saturday";  
nmonth+=1; add 1 to the month from the system
if (nyear<=99) nyear= "19"+nyear; put 19 in front of the year
if ((nyear>99) && (nyear<2000)) nyear+=1900;    check to see if it's over 2000
document.write("Today is "+nday+", "+nmonth+"-"+ntoday+"-"+nyear); write  Today is and the variables
} end the definition
</SCRIPT></head>  
<body bgcolor="white">  
<SCRIPT language="JavaScript"> write_date( ); </SCRIPT> activate the write_date ( ) function where the writing should go
</body>