JavaScript
  Home arrow JavaScript arrow Page 3 - 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 
Sun Developer Network 
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 - Cross-browser object instantiation: browser detection through exceptions


    (Page 3 of 5 )

    The simple script that you just saw to create XMLHTTP objects can be rewritten by using “try-catch” blocks for handling object instantiation. Even when this method may require a few additional lines of code, the overall script’s structure is more robust. Bearing in mind this concept, here’s what an exception-based script looks like:

    // XMLHttpRequest object detection using exceptions
    try{
        // browser is Firefox, Netscape, etc.
        // use XMLHttpRequest object
        var objXML=new XMLHttpRequest();
    }
    // catch thrown error
    catch(e){
        try{
            // browser is Internet Explorer
            // use ActiveX Control
            var objXML=new ActiveXObject("Microsoft.XMLHTTP");
        }
        // catch thrown error
        catch (e){
            alert('Ajax is not supported on your browser!');
        }
    }
    // open socket connection
    objXML.open('GET','samplefile.htm',true);
    // send request header
    objXML.setRequestHeader('Content-Type','text/html; charset=iso-
    8859-1');
    // send data
    objXML.send('var1=1&var2=2');

    Consider the above listed script, which now uses exceptions for instantiating the correct XMLHTTP object, according to the browser where the code is executed. In simple terms, what I’ve done is use a “try” block to wrap up the first line that attempts to create an XMLHTTP object, as depicted in the line below:

    try{
        var objXML=new XMLHttpRequest();
    }

    As you can see, if the pertinent object can be successfully created, it means that the browser natively supports it (Firefox, Nestcape and so forth). Otherwise, an exception will be thrown by the expression, being caught by the next “catch” block, in the following way:

    // catch thrown error
    catch(e){
        try{
            // browser is Internet Explorer
            // use ActiveX Control
            var objXML=new ActiveXObject("Microsoft.XMLHTTP");
        }
        // catch thrown error
        catch (e){
            alert('Ajax is not supported on your browser!');
        }
    }

    Similarly, the above “catch” block traps the raised error and tries to instantiate the object by using an ActiveX control (in the case of Internet Explorer). Due to the fact that the code responsible for instantiating the object is placed within another “try” block, any potential error can be easily handled through an additional “catch” block. Of course, if program control is moved to this last statement, it means that the browser doesn’t support XMLHTTP objects, therefore a warning alert is displayed.

    Even when the sample script you just saw requires additional code for checking object support, it looks more elegant and professional. It’s a matter of personal preference how you will implement cross-browser object creation in your programs.

    However, as you’ll probably agree, built-in exceptions provide a powerful mechanism for trapping most errors, including those ones that occurred when trying to create objects, so the above method should be appropriately considered.

    Having explained how exceptions can be used for addressing browser differences, it’s time to see another example. Therefore, the next case to be reviewed explains how to manipulate proprietary event handlers.

    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 Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT