2.6 在网页中添加脚本
现在已经完成了一个计算结果和显示的脚本,如程序清单2.1所示。
程序清单2.1 完整的日期和时间脚本
<script language="JavaScript" type="text/javascript">
now = new Date();
localtime = now.toString();
utctime = now.toGMTString();
document.write("<b>Local time:</b> " + localtime + "<BR>");
document.write("<b>UTC time:</b> " + utctime);
</script>
要使用此脚本,需要在HTML文档中加入此脚本。HTML文档最起码应该包括<html>标签、<head>标签和<body>标签的打开标签和结束标签。
如果把这些标签加入包含所写脚本的文档,并包含一些描叙性的文件头,其最后结果类似程序清单2.2。
程序清单2.2 HTML文档中的日期和时间脚本
<html>
<head><title>Displaying Times and Dates</title></head>
<body>
<h1>Current Date and Time</h1>
<p>
<script language="JavaScript" type="text/javascript">
now = new Date();
localtime = now.toString();
utctime = now.toGMTString();
document.write("<b>Local time:</b> " + localtime + "<BR>");
document.write("<b>UTC time:</b> " + utctime);
</script>
</p>
</body>
</html>
现在已经有一个完整的HTML文档了,将它保存成后缀为.htm或.html的文件。
|
|
注意:记事本和其他的Window文本编辑器默认将编写的脚本保存为扩展名为.txt的文件。要确保所存储的文件扩展名正确。 |







