Home arrow JavaScript arrow Page 2 - 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 - Using a Function for Checking Compatibility
(Page 2 of 4 )

Wrap the compatibility check inside a JavaScript function, then call this function before you make any HTTP requests using the object. For example, in Mozilla-based browsers such as Netscape 7.1 and Firefox 1.5 (as well as in Safari 2.0 and Opera 8.5), the request object is available as a property of the top-levelwindowobject. The reference to this object in JavaScript code iswindow.XMLHttpRequest. The compatibility check for these browser types looks like this:

  if(window.XMLHttpRequest){
     
request = new XMLHttpRequest();
  
request.onreadystatechange=handleResponse;
     
request.open("GET",theURL,true);
     
request.send(null);
 
}

The JavaScript variablerequest is to a top-level variable that will refer to the request object.

As an alternative model, the open-source library Prototype uses object-oriented JavaScript to wrap the request object into its own object, as in the object Ajax.Request (see Chapter 6).

If the browser supportsXMLHttpRequest, then:

  1. if(window.XMLHttpRequest)returnstruebecause theXMLHttpRequestis notnullorundefined.
  2. The object will be instantiated with thenew keyword.
  3. Itsonreadystatechangeevent listener (see the section “XMLHttpRequest” earlier in this chapter) will be defined as a function namedhandleResponse()
  4. The code calls the request object’sopen()andsend()methods.

What about Internet Explorer users?

Microsoft Internet Explorer–related blogs mentioned, at the time this book went to publication, that IE 7 would support a native XMLHttpRequest object.


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 10 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials