Customizing a Message for the Time of Day

You can take the technique used in the last example and use it again to customize a message for the user, depending on the time of day. This could be used, for instance, as a friendly greeting when a user enters a site. Script 12.3 shows how it is done, and Figure 12.3 shows how we were up writing way past our usual bedtime.

Figure 12.3. It was definitely too late at night when we wrote this.

To customize messages for the time of day:

  • if (theHour < 5) {
       return "What are you doing up so late?";

    We begin the new code in this script by starting a conditional test. Earlier in this script, the getHours() ...

Get JavaScript and Ajax for the Web: Visual QuickStart Guide, Seventh Edition 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.