Detect Browser Compatibility with the Request Object
(Page 1 of 4 )
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.
HACK 1 Detect Browser Compatibility with the Request Object
Use JavaScript to set up Microsoft’s and the Mozilla-based browsers’ different request objects.
Browser compatibility is an important consideration. You have to make sure the “engine” behind Ajax’s server handshake is properly constructed, but you can never predict which browsers your users will favor.
The programming tool that allows Ajax applications to make HTTP requests to a server is an object that you can use from within JavaScript code. In the world of Firefox and Netscape (as well as Safari and Opera), this object is namedXMLHttpRequest. However, continuing with the tradition established by IE 5.0, recent vintages of Internet Explorer implement the software as anActiveXobject namedMicrosoft.XMLHTTPorMsxml2.XMLHTTP.
Microsoft.XMLHTTP and Msxml2.XMLHTTP refer to different versions of software components that are a part of Microsoft XML Core Services (MSXML). Here’s what our contributing IE expert says on this matter.
“If you useMicrosoft.XMLHTTP, theActiveXObjectwrapper will try to initialize the last known good version of the object that has this program (or “prog”) ID. This object, in theory, could be MSXML 1.0, but almost no one these days has that version because it has been updated via Windows Update, IE 6, or another means. MSXML 1.0 was very short-lived. If you useMSXML2.XMLHTTP, that signifies to the wrapper to use at least MSXML 2.0 libraries. Most developers do not need to use a specific version of MSXML, such asMSXML2.XMLHTTP.4.0orMSXML2.XMLHTTP.5.0.”
Although Microsoft and the engineers on the Mozilla project have chosen to implement this object differently, we will refer to theActiveXandXMLHttpRequestobjects simply as “request objects” throughout this book, because they have very similar functionality.
As a first step in using Ajax, you must check if the user’s browser supports either one of the Mozilla-based orActiveX-related request objects, and then properly initialize the object.
Next: Using a Function for Checking Compatibility >>
More JavaScript Articles
More By O'Reilly Media
|
This article is excerpted from the book Ajax Hacks, written by Bruce W. Perry (O'Reilly; ISBN: 0596101694). Check it out today at your favorite bookstore. Buy this book now.
|
|