Home arrow JavaScript arrow Page 3 - 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 / 6
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

TOOLS YOU CAN USE

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.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



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