--== Basic Commands ==--
[ Previous Section | Home | Index | Test Your HTML | Related links | Next Section ]



Now that you know how to incorporate Java Script into your web page, the next step is to begin actually writing a script.

The first command to learn is how to print text directly onto the screen. There are two methods to do this (method refers to a Java Script method). Remember the lesson on objects. You learned that one of the built-in objects was the document object. The document object has a method for writing text in a web browser. The methods are write() and writeln(). The only difference between the two is that writeln() adds a space after the text and write() does not. To use the commands your Java Script would say document.write("the text you want to write"). The text you want outputted goes between the parenthesies and in quotes

Note: In Java Script both single quotes and double quotes have the same meaning, but you must use the same type in a pair. They cannot be mixed. In other words you can not open wi th double quotes and close with single quotes.

You can see an example of the write() and writeln() methods below:

<HTML>
<Head>
</Head>
<Body>


<Script Langua ge=JavaScript>
document.writeln("this is printed on the screen");
document.write('this is also printed');
document.wirte('notice no space');

</Script>
</Body>
</HTML>

note: Commands in Java Sc ript are seperated by a semi-colon. You can also use HTML tags in the write() method to format the text that you want to print.

Here is the result of the above code:




Another way to present information to the user is through pop-up boxes. These boxes are not part of the web page, but are generated by the Java Script. There are three different types of boxes. The first is the alert box which is designed to inform the user. It can be a warning or a thank you. To produce an alert box you add the line alert("what you want to say") to y our script. For this command the object is window. When window is the object, it doesn't have to be included because the browser assumes that it is there.




The next type of pop-up box is the prompt box. The prompt box prompts the user for information. You can use it to ask what day it is or what their favorite color is. To produce a prompt box add this code to your script prompt("what you want to say","default answer") The defualt answer is what is displayed in the box when it first appears.




The last type is the confirm box which is used to confirm a choice or action that user has made. The confirm box is the same as the alert box except it has two buttons, an "OK" and a "Cancel". If the user chooses the "OK" button it returns a true value, but if the user hits the "Cancel" button it returns a false value. As you learn more about Java Script you will learn how to use these returned values. For now you to use it in a script you would add confirm("What you want confirmed").


[ Previous Section | Home | Index | Test Your HTML | Related links | Next Section ]