Comments.
This is an HTML comment: <!-- Comment -->
This is a javascript
comment: /* Comment */
Everything between the /* and the */
is ignored. This can spread over multiple lines...
/************************************************* - This is all comments here. - You can write all kinds of stuff in a comment like this. **************************************************/
This is another javascript
comment: // Comment
It's a single line comment. Everything on that line after the //
is ignored.
document.write()
You can use javascript to write to the page...
<HTML> <HEAD> <TITLE></TITLE> </HEAD> <BODY> <SCRIPT language="Javascript"><!-- document.write("Hello Joe!"); //--></SCRIPT> </BODY> </HTML> |
Try it.
Technically, this is the write method of the document object. And technically it should be window.document.write(), but the browser is smart enough to know that the document is in a window so you can leave it off. Later on we'll be messing with other windows and frames, and then we'll start to worry about it. But for now, while we're only talking about a single document in a single window, we can skip the window part.
Can we output HTML tags?
<HTML> <HEAD> <TITLE></TITLE> </HEAD> <BODY> <SCRIPT language="Javascript"><!-- document.write("<CENTER><I><B>Hello Joe!</B></I></CENTER>"); //--></SCRIPT> </BODY> </HTML> |
Try it.
There is a potential problem here though. Consider the following...
<HTML> <HEAD> <TITLE></TITLE> </HEAD> <BODY> <SCRIPT language="Javascript"><!-- document.write("And God said, "Let there be light!""); //--></SCRIPT> </BODY> </HTML> |
Try it.
Hmm. Error.
If you're using a later version of Netscape, javascript errors may not be so apparent. Rather than an 'in your face' error window, the error may be quietly logged to a "javascript console" (not to be confused with the "java console"). The javascript console is accessed by typing javascript: in the location bar. This is where your error messages are.
The nested quotes are confusing the browser. The fix is simple... whenever you want quotes as part of a string you must escape them by preceeding them with a backwards slash. Escaping a character in a string basically tells the browser that a particular character is next (a quotation mark in this case) and that it's part of the string and not part of the script.
Look at the re-worked script...
<HTML> <HEAD> <TITLE></TITLE> </HEAD> <BODY> <SCRIPT language="Javascript"><!-- document.write("And God said, \"Let there be light!\""); //--></SCRIPT> </BODY> </HTML> |
Try it.
This document.write bit is a really handy thing to have around. With it you can dynamically print part of your page. For example, I know that your name is and you are years old. (Remember the info you gave earlier?)
Have a look at this example...
<HTML> <HEAD> <TITLE></TITLE> </HEAD> <BODY> <SCRIPT language="Javascript"><!-- myname = "Joe"; document.write(myname); //--></SCRIPT> </BODY> </HTML> |
Try it.
It simply writes the variable myname to the page.
Exercise: use a prompt box to get a name, then write it to the page like this... Hello Joe!
Here is a solution.
When writing to the page you can have as many document.write() statements as you wish...
document.write("<P><B>A poem for " + yourname + "...<BR></B>"); document.write("<I>Roses are red,<BR>"); document.write("Violets are blue,<BR>"); document.write("Javascript is fun,<BR>"); document.write("And so are you!,<BR></I>");
It works exactly like YOU writing to the page, except the script is doing the writing and you can insert variables. We will fiddle around with document.write() in later areas of this tutorial.
One more little bit that I want you to know about before we move on...
document.writeln()
document.write() simply writes each line tacked on to the end of the last. document.writeln() writes each line on a new line. Not super important unless you want to be able to read what the browser has written to the page. write() can produce a jumbled (though perfectly workable) mess. writeln() generates more tidy output.
Next>> | ||||||||||||||||||||||||||||||||
|