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 / 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 - 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

- 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