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()
code>. 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.