Example 2—Simple Clock

A clock is a favorite of many Web developers, who use it to add a little bit of dynamic content to a Web page. In this example (which works in both Internet Explorer and Netscape Navigator), the time is displayed in the input box (see Figure 15.2):

 <html> <head> <title>A Simple Page</title> <script language="JavaScript"> <!-- Cloaking device on! function gettime() { var date= new Date(); var hr = date.getHours(); var m = date.getMinutes(); var s = date.getSeconds(); var ampm = "AM"; if (hr > 11) { ampm = "PM" } if (hr > 12) { hr -= 12 } if(m < 10) { m = "0" + m } if(s < 10) { s = "0" + s } document.clockform.clock.value = hr + ":" + m + ":" + s + " " +ampm; setTimeout("gettime()",100) } // Cloaking device off --> </script> ...

Get JavaScript™ 1.5 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.