Simple Event Handling


© Muhammad Ali Shah

  • Level: Advanced
  • Skills Required: Functions, Events and Objects


Abstract:

The cyberspace is shifting from web pages to web applications. As a consequence the importance of scripting languages is increasing. JavaScript, being a part of this shift, started as a simple means of making web pages dynamic. Since then it has evolved to incorporate CSS (Cascade Style Sheets) and DHTML. Web sites made using advanced features of the scripting language look more like applications.

With this article we are starting a whole debate on advanced features of JavaScript - from event handling to DHTML. Our main focus will be on IE 5.x.


Why Internet Explorer?

The last three or four years have been extremely decisive about the web future. With the advent of Netscape 4.0, came version 1.2 of JavaScript. Meanwhile, Microsoft shifted to JScript - almost the same thing but with a different name. Along with that, the DOM (document object model) of the browsers changed so much that we almost always have to take sides. We, the web developers, are the sufferers.

Few people will deny that the browser war is over. Microsoft has clearly beaten Netscape in this battle. Now, since, Netscape has released version 6 of its browser, things may change a little bit (no flames intended). But as its known that Netscape 6 is almost a new product - its more complaint to W3C standards - the DOM is different from Netscape 4.7x - a nice move as far as I can predict.

To write pure browser independent code, the web developers have to test it on three browsers - Netscape 4.x, IE 5.x and Netscape 6. By far, the one most extensively used is IE 5.x. I can't predict the future but for now its a wise decision to support IE if you are a commercial developer and have to take sides. As the saying goes, "Write for the machine that has highest market."


Simple Event Handling:

You are handling events when you are responding to a mouse click (i.e. onMouseOver) or form submission (i.e. onSubmit). The simplest way to handle these events is to call a function for a particular element; something like this:

<a href="http://www.yahoo.com"
   onMouseOver="someFunction()">
   Click here</a>

This simple code calls the method someFunction() of yours when the mouse hovers above the link. Some events require a return true; statement at the end of the handler method so that they can proceed further. For example, we can have an event handler like this:

Go To Page: 1 2


The copyright of the article Simple Event Handling in JavaScript is owned by . Permission to republish Simple Event Handling in print or online must be granted by the author in writing.

Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo