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).
The object will be instantiated with thenew keyword.
Itsonreadystatechangeevent listener (see the section “XMLHttpRequest” earlier in this chapter) will be defined as a function namedhandleResponse().
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.