| home / programming / javascript / professional / chap4 / 3 |
[previous] [next] |
|
Timer EventsThere are ways of causing a JavaScript script to run other than with an HTML intrinsic event. The JavaScript function setTimeout() is an easy alternative:
This page displays an alert box to the user every two seconds. The first time this occurred because the function wake_up() was scheduled in the second last line when the document was displayed. It reoccurred because the function reschedules itself just before it ends. The first argument to setTimeout() can be any valid JavaScript. If you run this example, you'll see that timers created with setTimeout() can be really irritating, but they're good at checking the state of a document or window, performing animation, or regularly reloading a document in case it has changed. This last case is a variant of 'client pull' behavior where the browser regularly updates itself without the user doing anything. The setTimeout() function only acts once - if you want it to repeat you must reset it each time, as in this example, or change tactics and use a similar timer that does repeat, called setInterval(). Plug-in and ActiveX EventsWhenever plug-ins or ActiveX controls are engaged handling <EMBED> and <OBJECT> tags in HTML, JavaScript can also happen. This is because LiveConnect and ActiveX allow plugins and controls to run specific JavaScript functions or scripts. For Netscape, the <EMBED> tag must include the MAYSCRIPT attribute, or this kind of event won't be allowed. The special file type identified by these tags can actually contain bits of JavaScript script as part of its content. See Chapter 6 on Multimedia and Plugins for more details. Java Exception EventsA mechanism similar to HTML events is to use Netscape's BeanConnect technology in Netscape 4.0 browsers. This allows JavaScript code to be invoked when a Java exception occurs in an applet, without a lot of extra effort required when the applet is written. Java exceptions are highly flexible things that can happen almost any time. Microsoft VariationsMicrosoft provides a bit of syntactic sugar to the JavaScript language that saves you from having to define and assign an event handler in two separate steps. This syntax allows you to prefix the function name with the name of the object it will be a method of (recall all functions are methods of some objects anyway). This works for Internet Explorer 4.0+
Of course this doesn't save you much if you need to assign the function to multiple handlers. There are other tactics you can use for that, but first you need to understand the way events are processed, see later in this chapter. Other Odditieswatch() and unwatch() object methods are features aimed at easing JavaScript debugging, but they're only available in Netscape 4.0+ browsers. Apart from debugging, they do provide a general mechanism for calling an otherwise unconnected function when a plain object property has its value changed. This is not strictly an event-oriented mechanism. Chapter 17, Debugging illustrates the most common use of this feature. Finally, Chapter 9 describes how dynamic updates to the browser's preferences can occur via JavaScript without any user interaction at all. Adding JavaScript to News and E-mail MessagesBrowsers at the 4.x+ level include the ability to send e-mail messages and news posts in HTML format. For Internet Explorer 4+, the e-mail client is Microsoft Outlook, or Outlook Express. Where HTML goes, JavaScript can go as well since the e-mail or newsreader that the message is read with contains a JavaScript interpreter and HTML renderer. Then the message in HTML format will run any JavaScript contained within it as would normally happen with an HTML-and-JavaScript web document. This raises a number of interesting possibilities, which are reminiscent of Lotus Notes and other workflow style products:
The techniques to use here are the same as those for scripted HTML documents in general, which are covered in later chapters. If the reader of the message doesn't have a JavaScript/HTML enabled reader, all the JavaScript code and HTML tags will appear intermixed with the content intended for the reader - plain text is still the safest for e-mail and news conversations with the rest of the world. |
| home / programming / javascript / professional / chap4 / 3 |
[previous] [next] |
Revised: March 21, 2001
Created: March 21, 2001