|
| You are here: Categories » Computers and technology » AJAX and JavaScript
|
One of the attributes that have made XMLHttpRequest such a popular transport for AJAX requests is that it is easy to use in a way that is compatible across multiple browsers. The big two browsers, IE and Firefox, provide the same basic API. This consistency makes for a similar development experience. Opera and Safari also support the same basic API, but only in their more recent versions.
When you are writing cross-browser, the first problem you need to overcome is that XMLHttpRequest is an ActiveX object in IE, and it's a normal JavaScript object in Mozilla and the other browsers. There are a number of approaches to overcoming this problem, including optional JScript code for IE, but I find that the simplest solution is just to use exceptions. Below is an example that tries every version of the XMLHTTP ActiveX object, if needed. This helps make our implementation as robust as possible. The function also throws an exception if it's not possible to create an XMLHttpRequest object. This gives us a way to give error messages or to fall back to IFrame requests, if needed.
Cross-Browser XMLHttpRequest Creation
1 // function to create an XMLHttpClient in a cross-browser manner
2 function initXMLHttpClient() {
3 var xmlhttp;
4 try {
5 // Mozilla / Safari / IE7
6 xmlhttp = new XMLHttpRequest();
7 } catch (e) {
8 // IE
9 var XMLHTTP_IDS = new Array('MSXML2.XMLHTTP.5.0',
10 'MSXML2.XMLHTTP.4.0',
11 'MSXML2.XMLHTTP.3.0',
12 'MSXML2.XMLHTTP',
13 'Microsoft.XMLHTTP' );
14 var success = false;
15 for (var i=0;i < XMLHTTP_IDS.length && !success; i++) {
16 try {
17 xmlhttp = new ActiveXObject(XMLHTTP_IDS[i]);
18 success = true;
19 } catch (e) {}
20 }
21 if (!success) {
22 throw new Error('Unable to create XMLHttpRequest.');
23 }
24 }
25 return xmlhttp;
26 }
|
The overall pattern of this code is simple: Create an XMLHttpRequest instance in the most optimal way possible, as shown in line 6. This creation should always succeed on Mozilla-based browsers, such as Firefox, on Opera, and on the upcoming IE 7.
If XMLHttpRequest doesn't exist, catch the exception that is thrown, as shown in line 7. Getting an exception means you're on IE or an old browser. To test for IE, attempt to create an ActiveX version of XMLHttpRequest, which is accomplished by the following:
-
Looping over all possible ActiveX identifiers. This action will create an ActiveX instance for each identifier until the creation succeeds, setting the success flag to TRue, as shown in lines 920.
- If creation is successful, returning an XMLHttpRequest instance, as shown in line 25. Otherwise, throwing a JavaScript exception, as shown in line 2
This approach allows for minimal overhead if the browser supports a native XMLHttpRequest object while fully supporting IE. It also gives us an error if XMLHttpRequest isn't supported at all. This error could be displayed to the user at this point, or you could insert another communication approach, such as hidden IFrames.
 |
|
Leave a comment or ask a question
|
|
Total comments: 0
Disclaimer
- The e-articles directory is not responsible for any and all copyright infringements by writers and authors. If you suspect the information contained by this page for any copyright infringements, please contact us to investigate the issue
|
|
|
What Is Ajax - Ajax stands for Asynchronous Javascript And XML. Although strictly speaking Ajax itself is not a technology, it mixes well-known programming techniques in an uncommon way to enable web develo (more...)
XMLHttpRequest Overview - Originally, Microsoft designed XMLHttpRequest to allow Internet Explorer (IE) to load XML documents from JavaScript. Even though it has XML in its name, XMLHttpRequest really is a generic HTTP (more...)
Goals of AJAX - First and foremost, AJAX is about improving user experience; user experience improvements fall into two categories: making current tasks easier and making previously impossible tasks possible. (more...)
JavaScript Lives in a Web Page - All the code that you write for JavaScript goes into an HTML page. If you don't know HTML yet, you should run out and get a good book on HTML. Lynda and William Weinman's (more...)
JavaScript Literals - The raw data that make up the root of data types are called "literals." These are, in effect, literally what they represent themselves to be. Numbers, strings, and Boolean values make up the cor (more...)
JavaScript Variables - I like to think of variables as containers on a container ship. You can put all different types of content into the containers, move them to another port, empty them, and then replace the contain (more...)
JavaScript Operators - Operators can be placed into three categories-binary, unary, and ternary. Binary operators, most commonly associated with the concept of operator, take two (binary) expressions and combine them in (more...)
JavaScript Arrays - Because objects are collections of properties with each property having its own name and value, arrays are actually JavaScript objects. Each property in an array is an element, and each element (more...)
|
|
|