JavaScript
  Home arrow JavaScript arrow Page 2 - 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 - JavaScript-based http requests: implementing cross-browser object instantiation


    (Page 2 of 5 )

    Being the largest development platform available today, the Web is certainly a huge area where new technologies are constantly emerging, which bring to developers a broad range of possibilities to largely expand the capacity of web-based applications. Undoubtedly, one of the most popular technologies currently used to build richer programs is AJAX (short for Asynchronous JavaScript and XML).

    If you’re not aware of this, JavaScript provides you with the ability to perform http requests without page reloads, and receive the server’s response to boost a vast amount of service and utilities, which can be run silently in the background. As a result, many JavaScript application layers have introduced the use of XMLHttpRequest objects to make requests to a given host and handle the corresponding responses accordingly.

    Of course, and not surprisingly, browser incompatibilities crop up when trying to instantiate XMLHTTP objects, since Internet Explorer implements this object through an ActiveX control, while other browsers, such as Firefox and Nestcape, offer native support for it. This situation obligates developers to write some additional checking code to make sure that cross-browser object instantiation is performed within a program.

    With reference to the above deployed concepts, a typical JavaScript snippet for making XMLHTTP objects available within cross-browser applications may look like this:

    // traditional XMLHttpRequest object detection
    if (window.XMLHttpRequest){
        // browser is Firefox, Netscape, etc.
        objXML=new XMLHttpRequest();
    }
    else if(window.ActiveXObject){
        // browser is Internet Explorer
        objXML=new ActiveXObject('Microsoft.XMLHTTP');
    }
    else{
        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');

    Basically, what the above script does is instantiate an XMLHTTP object, by adding some checking lines within the code to make sure that the object will be created, no matter what browser the program is running on. The rest of the script opens up a socket connection in order to request a given file and sends out the http request using the “GET” method.

    As you can see, due to the fact that XMLHTTP objects are not implemented in the same way by different browsers, a couple of “if-else” blocks are used to solve the incompatibility. It’s precisely for that reason that exceptions can be used as an alternative method for addressing browser inconsistencies. Because most JavaScript parsers will throw an exception whenever an object is unrecognized or unsupported, this condition can be handled through “try-catch” blocks and program flow can be moved to appropriate code, responsible for instantiating XMLHTTP objects.

    Considering that cross-browser object instantiation can be implemented using exceptions, let’s move on to see how this process is performed.

    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

    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in
    - Active Client Pages at the Server
    - ACP Tab Web Page







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT