JavaScript
  Home arrow JavaScript arrow Page 4 - Exception Handling in JavaScript: Addressi...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Exception Handling in JavaScript: Addressing Browser Incompatibilities
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2005-10-11

    Table of Contents:
  • Exception Handling in JavaScript: Addressing Browser Incompatibilities
  • JavaScript-based http requests: implementing cross-browser object instantiation
  • Cross-browser object instantiation: browser detection through exceptions
  • Proprietary event handlers: implementing browser-independent event handling
  • Assigning events to objects: using exceptions to manipulate event handlers

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More JavaScript Articles
    More By Alejandro Gervasio


       · The last part of the series uses JavaScript exceptions for addressing some browser...
       · Wouldn't you agree that exceptions are not for a regular control flow and should be...
       · Thank you for posting your comments on my JavaScript article. Yeap, I agree with you...
     

    JAVASCRIPT ARTICLES

    - Using jQuery to Preload Images with CSS and ...
    - Using Client-Side Scripting to Preload Image...
    - Removing Non-Semantic Markup when Preloading...
    - Using the Display CSS Property to Preload Im...
    - Preloading Images with CSS and JavaScript
    - Scaling and Moving Web Page Elements with th...
    - Fading, Hiding and Sliding HTML Elements wit...
    - Toggling CSS Properties with the GX JavaScri...
    - Cancel, Queue and Pause Animations with the ...
    - Producing Elastic Effects with the GX JavaSc...
    - Moving Divs Diagonally with the GX JavaScrip...
    - Moving Elements Vertically and Horizontally ...
    - Making Bouncing Effects in Parallel with the...
    - Creating Bouncing Effects with the GX JavaSc...
    - Manipulating Background Colors with the GX J...







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 10 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek