Course Technology logoCourse Technology
Tutorial 8

*
 Online 
    Companion 
 Home 

directory folder
Documents 

 HTML: 
 Tutorial 1 
 Tutorial 2 
 Tutorial 3 
 Tutorial 4 
 Tutorial 5 
 Tutorial 6 
 Tutorial 7 
 Tutorial 8 
 Tutorial 9 
 Tutorial 10 


DHTML: 
 Tutorial 1 
 Tutorial 2 
 Tutorial 3 
 Tutorial 4 
 Tutorial 5 


directory folder
Gallery 

directory folder
Software 


directory folder
Data Files 


directory folder
Reference: 

HTML Tags 

 Appendix D: 
 JavaScript 
 Objects, 
 Properties, 
 Methods, 
 Event 
 Handlers 

 Appendix E: 
JavaScript 
 Operators, 
 Elements, 
 Keywords 
  
 Appendix F: 
 Cascading 
 Style Sheets 



Web Pages and HTML icon

Programming  with
JavaScript

Creating a Programmable Web Page

Additional Topics

*JavaScript Resources on the Web
*Comparison Operators
*Interrupting Loops
*Extending Strings with the += Operator



JavaScript Resources on the Web

----
If you want more information on using JavaScript, here are some resources on the Web:
Ask the JavaScript Pro
DevEdge Javascript Reference
Joy of JavaScript, A Companion Site for
   Javascript for the World Wide Web, Visual Quickstart
   Guide, 3rd Edition, by Tom Negrino and Dori Smith
JavaScript World
JavaScript Forum
The JavaScript Planet
JavaScript Resources
JavaScript World
JavaScript WebRing
Netscape's JavaScript Guide
ProjectCool: JavaScript Zone

To the top

Comparison Operators

----
Another type of operator not discussed in Tutorial 7 is the Comparison operator. The Comparison operator is used to change the value of a variable based on a conditional expression. The syntax of the Comparison operator is:

variable = (condition) ? value1 : value2;

where variable is the variable name, condition is a conditional expression that is either true or false, value1 is the value of the variable if the condition is true, and value2 is the value if the condition is false. For example, the following JavaScript command:

Days = (Month=="Feb") ? "< 30" : ">= 30";

The variable Days will be equal to "< 30" if the Month variable is "Feb", otherwise it will be equal to ">= 30".

To the top

Interrupting Loops

----
If you are creating a loop, you can use the break and continue commands to alter the loop's operation.

The break command halts the loop, even if the conditions to the end the loop have not been met. For example, if you want to create a loop that prompts students to answer a question, and give them three chances to answer that question, you can use the following JavaScript program:

for (i=1;i<=3;i++) {

  answer=prompt("In what year did 

  the Civil War begin?","");

      if (answer=="1861") {

     alert("Correct answer");

   break;

  }<

}

In this example, if the answer variable is equal to "1861" (the correct answer) the loop stops with the break command. If the student answers the question incorrectly, the loop continues for 3 iterations or until the question is answered correctly (note that you can learn more about the alert() and prompt() methods in Tutorial 8.) To see this JavaScript program in action, click the button below:

The continue command is similar to the break command, except that it is used to jump to the end of the command block without completing the remaining commands in the block. From that point, the loop continues to iterate. For example, the following JavaScript program prompts a student to answer a series of simple math questions. If the student answers correctly, the program jumps to the end of the command block-skipping a command to displays the correct answer.

for (i=1;i<=5;i++) {

  answer=eval(prompt("What is "+i+" times "+i+"?",""));

   if (answer==i*i) {

   continue;

   }

    Alert("No, the answer is: "+i*i);

 }

To see this JavaScript program in action, click the button below:

To the top

Extending Strings with the += Operator

----
JavaScript commands must all be confined to a single line, so what do you do when you have a long text string that cannot be confined to a single line? One technique is to extend a text string value with the += operator. Say that you want to set the value of the quote variable to a long quotation from Shakespeare's Tempest; here's one way of doing it:

<SCRIPT>
var quote = "Be not afraid: the isle is full of noises, ";
quote+="sounds and sweet airs that give delight and hurt not. ";
quote+="Sometimes a thousand twangling instruments will hum ";
quote+="about mine ears; and sometime voices that, ";
quote+="if I then had waked after long sleep; ";
quote+="will make me sleep again; and then, in dreaming, ";
quote+="the clouds methought would open and show riches ready ";
quote+="to drop upon me, that, when I waked, ";
quote+="I cried to dream again. ";
document.write(quote);
</SCRIPT>

Here is the output from that script:

Be not afraid: the isle is full of noises, sounds and sweet airs that give delight and hurt not. Sometimes a thousand twangling instruments will hum about mine ears; and sometime voices that, if I then had waked after long sleep will make me sleep again; and then, in dreaming, the clouds methought would open and show riches ready to drop upon me, that, when I waked, I cried to dream again.

Notice that by using the += operator, you've created a text string much longer than the length of a single line.

To the top


*Online Companion Home  
*Software  *Gallery  directory folderData Files  
*Documents: HTML: Tutorial 1 | Tutorial 2 | Tutorial 3 | Tutorial 4
Tutorial 5 | Tutorial 6 | Tutorial 7 | Tutorial 8 | Tutorial 9 | Tutorial 10
DHTML: Tutorial 1 | Tutorial 2 | Tutorial 3 | Tutorial 4 | Tutorial 5

*Reference:  HTML Tags, Properties | Appendix D:Web Pages and HTML icon
JavaScript Objects, Properties, Methods, Event Handlers
Appendix E:  JavaScript Operators, Elements, Keywords
Appendix F:  Cascading Style Sheets

To the top

*
Website design by SKDesigns.Course Technology © 2000*
*