Home arrow JavaScript arrow Page 2 - Exception Handling in JavaScript: Addressing Browser Incompatibilities

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

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');
    alert('Ajax is not supported on your browser!');
// open socket connection
// send request header
objXML.setRequestHeader('Content-Type','text/html; charset=iso-
// send data

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.

blog comments powered by Disqus

- 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 

Developer Shed Affiliates


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