Home arrow JavaScript arrow Page 3 - Detect Browser Compatibility with the Request Object
JAVASCRIPT

Detect Browser Compatibility with the Request Object


If you use AJAX to improve your visitors' experience when they browse your web site, you may want to try out the hack described in this article. It helps with browser compatibility considerations. This article is excerpted from chapter one of the book Ajax Hacks, written by Bruce W. Perry (O'Reilly; ISBN: 0596101694). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 5 stars5 stars5 stars5 stars5 stars / 2
July 17, 2008
TABLE OF CONTENTS:
  1. · Detect Browser Compatibility with the Request Object
  2. · Using a Function for Checking Compatibility
  3. · Ajax Basics
  4. · Ajax Basics continued

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Detect Browser Compatibility with the Request Object - Ajax Basics
(Page 3 of 4 )

In this case, thewindow.XMLHttpRequestobject will not exist in the browser object model. Therefore, another branch of theiftest is necessary in your code:

  else if (window.ActiveXObject){
     
request=new ActiveXObject("Microsoft.XMLHTTP");
      if (! request){
         
request=new ActiveXObject("Msxml2.XMLHTTP");
      }
     
if(request){
  request.onreadystatechange=handleResponse;
            request.open(reqType,url,true);
            request.send(null);
      }
  }

This code fragment tests for the existence of the top-levelwindow objectActiveXObject, thus signaling the use of Internet Explorer. The code then initializes the request using two of a number of possibleActiveXprogram IDs (here,Microsoft.XMLHTTPandMsxml2.XMLHTTP).

You can get even more fine-grained when testing for different versions of the IE request object, such asMsxml2.XMLHTTP.4.0. In the vast majority of cases, however, you will not be designing your application based on various versions of the MSXML libraries, so the prior code will suffice.

The code then makes one final check for whether the request object has been properly constructed (if(request){...}).

Given three chances, if therequestvariable is stillnullorundefined, your browser is really out of luck when it comes to using the request object for Ajax!


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



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