Home arrow JavaScript arrow Page 4 - Exception Handling in JavaScript: Addressing Browser Incompatibilities
JAVASCRIPT

Exception Handling in JavaScript: Addressing Browser Incompatibilities


Not all browsers handle all code the same way. Differences can be addressed with JavaScript exceptions. These may not be the best way under all circumstances, but you'll find it worthwhile to examine this approach.

Author Info:
By: Alejandro Gervasio
Rating: 4 stars4 stars4 stars4 stars4 stars / 11
October 11, 2005
TABLE OF CONTENTS:
  1. · Exception Handling in JavaScript: Addressing Browser Incompatibilities
  2. · JavaScript-based http requests: implementing cross-browser object instantiation
  3. · Cross-browser object instantiation: browser detection through exceptions
  4. · Proprietary event handlers: implementing browser-independent event handling
  5. · Assigning events to objects: using exceptions to manipulate event handlers

print this article
SEARCH DEVARTICLES

Exception Handling in JavaScript: Addressing Browser Incompatibilities - Proprietary event handlers: implementing browser-independent event handling
(Page 4 of 5 )

One of the most requested features for the upcoming version of Internet Explorer is full support for the W3CDOMstandard “addEventListener()” method, which unfortunately is not implemented by Microsoft’s current browsers. Since Internet Explorer exposes its own “attachEvent()” method for tying events to objects, browser-independent event handling is very often achieved through sniffing functions, which use the proper event handling method, according to the browser that the script is executing on.

A typical script that deals with both event handlers may be written like this:

// regular Event Handler assignation
var obj=document.getElementsByTagName('div')[0];
if(!obj){alert('Failed to get object!')};
if(obj.addEventListener){
    // browser is Firefox, Netscape, etc.
    // use addEventListener() method
    obj.addEventListener("click",processEvent,false);
}
else if(obj.attachEvent){
    // browser is Internet Explorer
    // use attachEvent() method
    obj.attachEvent('onclick',processEvent);
}
else {
    alert('Failed to add event handler');
}

As you can see, the above fragment of code uses some checking lines to determine what handling method will be attached to a regular <div> element, in order to call the “processEvent()” function when the element is clicked on. Given that, this function might be defined as simply as this:

function processEvent(){
    alert('Event handler successfully attached!');
}

Using this technique, it’s possible to encapsulate the whole code into a generic function that solves internally the described incompatibilities and attaches events to objects as transparently as possible. In this case, I’d like to deliberately maintain the code out of wrapping functions though, so you can study it and decide the best approach to be taken.

For most developers, the above example is extremely simple and the code is pretty self explanatory. Certainly, the only thing worth considering is that similar results can be obtained with exceptions. Keeping in mind the same logic that I applied to creating XMLHTTP objects, it’s possible to solve browser differences when working with event handlers. So, the next step will consist of demonstrating an alternative method to assign events to objects, this time using exceptions.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials