HTML and CSS Reference
In-Depth Information
basic ways in JavaScript to send output to a Web document. In this case, you've used the
write() method to write new text into the document object. The document.write()
method has the general syntax
document.write(“ text ”);
where text is a string of characters that you want written to a Web document. The
text string can include HTML tags. For example, the following statement writes the text
Monroe Public Library marked as an h1 heading into a document:
When a browser encounters this statement, it places the text and the markup tags in
the document and renders that text as if it had been entered directly into the HTML file.
Kate wants the e-mail addresses in the staff directory to appear as hypertext links.
This requires placing the e-mail addresses within <a> tags and adding the href attribute
value indicating the destination of each link. For example, the code to create a link for
Catherine Adler's e-mail address is
Another method to write
text to a Web page is the
method which places the
text in a separate block,
while the document.
write() method places
the text in-line with
other content.
Writing this text string requires you to include quotation marks around the href
attribute value. Because text strings created with the document.write() method must
be enclosed in quotes as well, you have to place one set of quotes within another. This
is done by using both single and double quotation marks. If you want to write a double
quotation mark as part of the code sent to the document, you enclose the quotation
marks within single quotation marks. To write single quotation marks, you enclose them
within a set of double quotation marks. The type of quotation mark to be written to the
Web page must always be different from the type of quotation marks that enclose it. If
you try to enclose double quotes within another set of double quotes, browsers won't
know where the quoted text string begins and ends. The following JavaScript code
encloses the href attribute value in single quotes and uses double quotes to mark the
entire text to be written to the Web page document:
Note that this example places the entire code into three separate document.write()
commands. Although you could use one long text string, it might be more difficult to
read and to type without making a mistake. A browser treats these consecutive com-
mands as one long string of text to be written into the document.
Writing to a Web Page
• To write text to a Web page with JavaScript, use the method
where text is the HTML code to be written to the Web page.
You're ready to add the code for the link to Catherine Adler's e-mail address.
Search WWH ::

Custom Search