Simple Event Handlingfunction handler() { if (form is valid) return true; else return false; } This pseudo-code may be called with an onSubmit() event of the form. Thus, the handler() will be called when the form is being submitted. If the method returns true the form will be submitted. But if the returned value is false the form will not be submitted. Similarly an onClick can be programmed such that the link may work under some conditions and not otherwise.
Whatever we have seen is element level event handling. The handler must explicitly be called for each object. But the beauty of JavaScript is that it allows you to define document and window level event handlers. These events can be fired when there is a mouse click or key press anywhere on the page, etc. But at this point, the two browser take different paths and its very difficult to write cross browser code. We will continue this discussion with IE in mind in future articles.
We have just seen the basics of events and event handler with some discussion of what lies ahead. Keep your fingers crossed for the next articles on the exciting topics of DHTML and advance JavaScripting.
The copyright of the article Simple Event Handling in JavaScript is owned by Muhammad Ali Shah. Permission to republish Simple Event Handling in print or online must be granted by the author in writing.
Go To Page: 1 2 Articles in this Topic Discussions in this Topic |