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.
Next: Cross-browser object instantiation: browser detection through exceptions >>
More JavaScript Articles
More By Alejandro Gervasio