HTML and CSS Reference
In-Depth Information
You can also use the + symbol to combine a variable with a text string and then write
the combined text string to the document. The following statements use the libName
variable and the
ƒdocument.write()
method to write the text string
<p>Welcome to the
Monroe Library</p>
to the Web document:
varƒlibNameƒ=ƒ“MonroeƒLibrary”;
document.write(“<p>Welcomeƒtoƒtheƒ“ƒ+ƒlibNameƒ+ƒ“</p>”);
You can use the
document.write()
method with the variables you've already created
to write the hypertext link for Catherine Adler's e-mail address. The code is as follows:
document.write(“<aƒhref='mailto:”ƒ+ƒemLinkƒ+ƒ“'>”);
document.write(emLink);
document.write(“</a>”);
If the text string
cadler@mpl.gov
is
stored in the emLink variable, these commands
will write the following HTML to the Web page:
<aƒhref='
mailto:cadler@mpl.gov'>
</a>
Notice that the
document.write()
method nests single quotes within double quotes
so that the HTML code written to the Web page places the value of the
href
attribute
within a set of single quotation marks. You'll add this JavaScript code to the Web page
now, replacing the previous commands.
To replace the
document.write()
commands in the script:
◗
1.
Return to the
mpl.htm
file in your text editor.
◗
2.
Replace the three
document.write()
statements in the script with the following
code, as shown in Figure 10-15:
Make sure that you
enclose single quotes
within a set of double
quotes so that the single
quotes appear in the
rendered HTML code.
document.write(“<aƒhref=
'
mailto:”ƒ+ƒemLinkƒ+ƒ“
'
>”);
document.write(emLink);
document.write(“</a>”);
Figure 10-15
Writing the value of the emLink variable to the Web page
◗
3.
Save your changes to the file and then reload
mpl.htm
in your Web browser. The
hypertext link for Catherine Adler's e-mail address should remain unchanged
from what was shown earlier in Figure 10-11.