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.
You can use javascript to write to the page...
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
document.write("Hello Joe!");
</script>
</body>
</html>
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 type="text/javascript">
document.write("<center><i><b>Hello Joe!</b></i></center>");
</script>
</body>
</html>
There is a potential problem here though. Consider the following...
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
document.write("And God said, "Let there be light!"");
</script>
</body>
</html>
Hmmm. Error.
With some browsers/configuations, errors may not be so apparent. You may see a popup error window, or the error may be silent.
Internet Explorer based browsers may or may not throw up an error box. Either way though, in the case of a javascript error, you'll see a little yellow icon in the lower left corner of the window. Clicking on that will pop up the error message.
Firefox users can access a "javascript console" by clicking Tools/JavaScript Console on the menu. It can also be accessed by typing javascript: in the location bar. Netscape users don't have a menu item like Firefox, but they too can type javascript: into the location bar to bring up the console.
Ok, back to our error. 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 type="text/javascript"> document.write("And God said, \"Let there be light!\""); </script> </body> </html>
This document.write() bit is a very 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. ()
Have a look at this example...
<html> <head> <title></title> </head> <body> <script type="text/javascript"> myname = "Joe"; document.write(myname); </script> </body> </html>
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!
When writing to the page you can have as many document.write() statements as you wish...
document.write("<p>"); document.write("<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!</i>"); document.write("</p>");
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, where 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.
Javascript Tutor |
Lessons: Intro 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
Javascript Authoring Guide JavaScript FAQ |
HTML 4.0 Reference Google Groups (Advanced Search) |
![]() |