<HTML>
<HEAD>
<TITLE>Random quote</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
function getMessage() {
// create array of Murphy's laws
var ar = new Array(20)
ar[0] = "Nothing is as easy as it looks."
ar[1] = "Everything takes longer than you think."
ar[2] = "Anything that can go wrong will go wrong."
ar[3] = "If there is a possibility of several things going
wrong , the one that will cause the most damage will be the
one to go wrong."
ar[4] = "If there is a worse time for something to go wrong, it will
happen then."
ar[5] = "If anything simply cannot go wrong, it will anyway."
ar[6] = "If you perceive that there are four possible
ways in which a procedure can go wrong, and circumvent
these, then a fifth way, unprepared for, will promptly develop."
ar[7] = "Left to themselves, things tend to go from bad to worse."
ar[8] = "If everything seems to be going well, you have obviously
overlooked something."
ar[9] = "Nature always sides with the hidden flaw."
ar[10] = "Mother nature is a bitch."
ar[11] = "It is impossible to make anything foolproof
because fools are so ingenious."
ar[12] = "Whenever you set out to do something,
something else must be done first."
ar[13] = "Every solution breeds new problems."
ar[14] = "Trust everybody ... then cut the cards."
ar[15] = "Two wrongs are only the beginning."
ar[16] = "If at first you don't succeed, destroy all evidence that
you tried."
ar[17] = "To succeed in politics, it is often necessary
to rise above your principles."
ar[18] = "Exceptions prove the rule ... and wreck the budget."
ar[19] = "Success always occurs in private, and failure in full view."
var now = new Date()
var sec = now.getSeconds()
alert("Murphy's Law:\r" + ar[sec % 20])
}
//-->
</SCRIPT>
</HEAD>
<BODY onLoad="getMessage()">
</BODY>
</HTML>
Example 14-4 (ex14-4.htm). A script to display a random message each time the
page is loaded.
The first statement in the function creates an array, an instance of the built-in Array
object. The array includes 20 elements, starting from ar[0] and ending with ar[19].
Each element is assigned a string, or to be exact, a Murphy law. An instance of the Date
object, now, is then created. The number of seconds in the current time is
retrieved from now via the getSeconds() method. As you know, the value
of sec is an integer between 0 and 59, with a random possibility for each. In
total there are 60 consecutive integers. Due to this fact, the expression sec % 20
returns an integer between 0 and 19, with an equal chance for each, because 60 is
divisible by 20 (60 / 20 = 3!). The ability to create a random number between 0 and 19
using this technique enables us to randomly choose a Murphy law from the array. The
selected Murphy law is displayed by an alert box.
The most important part of the script to pay attention to is the use of an event
handler to respond to the load event—onLoad. When the event handler is
triggered (when the page has completed loading), the function getMessage() is
called to display an alert message as described earlier. Just so you can get a feel for
the script, here is a sample output:
Also notice the use of an escape sequence, the carriage return character (\r).
Summary
In this chapter we discussed date- and time-related concepts in JavaScript. Such
concepts are based on the Date object. We learned about this built-in object, the
only one in JavaScript without any properties. After a deep look through the object, we
saw how its instances can be created and used in scripts. By now, you should have enough
tools to create interesting scripts of various types. As we look further into the language
in the following chapters, you will be able to grasp the power of JavaScript to enhance
HTML documents. In this chapter you have seen that without much effort, you can add a
graphical digital clock to any page. This may seem amazing, but the best is yet to come,
including animated clocks, and more.