spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / javascript / professional / chap4 / 3 To page 1To page 2current pageTo page 4
[previous] [next]

Professional JavaScript

Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

Timer Events

There are ways of causing a JavaScript script to run other than with an HTML intrinsic event. The JavaScript function setTimeout() is an easy alternative:

<HTML><HEAD><SCRIPT>
function wake_up()
{
  alert('Fire, Flood, Famine!');              // Try to wake up the user.
  setTimeout('wake_up()', 2000);
}
setTimeout('wake_up()', 2000);                // 2000 milliseconds in the future
</SCRIPT></HEAD></HTML>

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 Events

Whenever 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 Events

A 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 Variations

Microsoft 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+

Function document.shouter.last.onchange()
{
. . . // same content
}

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 Oddities

watch() 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 Messages

Browsers 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:

  • Including scripts that advise you when your e-mail has been read at the other end.
  • Providing forms and surveys for readers to fill out.
  • Demonstrating scripts to others.
  • Producing multimedia messages, including 'spoken' audio messages.

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 To page 1To page 2current pageTo page 4
[previous] [next]

Copyright 1999 (1st Edition) and 2001 (2nd Edition) Wrox Press Ltd. and

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Symantec Whitepaper: Converging System and Data Protection for Complete Disaster Recovery
Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
  Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Symantec Whitepaper: Comprehensive Backup and Recovery of VMware Virtual Infrastructure
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Fixing MySQL Replication · Firewall Guide: First Steps to Securing the Enterprise · VoxOx Tames the Tumultuous Communications Tangle


Revised: March 21, 2001
Created: March 21, 2001


URL: http://webreference.com/programming/javascript/professional/chap4/3/