Ajax| Browser
AJAX Hacks HACK1. To detect browser compatibility
This section describes how to use JavaScript to establish a corresponding request object for IE or Mozilla browsers. The browsers used by the client are various. Therefore, there are also different request objects. As in Firefox, Netscape, Safari,opera is XMLHttpRequest. IE is microsoft.xmlhttp or msxml2.xmlhttp.
The first step in using AJAX is to detect the type of client browser and get the request object based on the appropriate type. Here's the complete code to get the object:
/* Initialize A Request object is already constructed /
function Initreq (reqtype,url,bool) {
/ Specify the function that would handle the HTTP response * *
Request.onreadystatechange=handleresponse;
/* onReadyStateChange listens for handleresponse functions. The next step is to open the connection and send the request ...
*/
Request.open (Reqtype,url,bool);
Request.send (NULL);
}
/* Wrapper function for constructing a Request object.
Parameters:
Reqtype:the HTTP request type such as Get or POST.
Url:the URL of the server program.
Asynch:whether to send the request asynchronously or not. */
function HttpRequest (reqtype,url,asynch) {
mozilla-based Browsers
if (window. XMLHttpRequest) {//If non-IE browser
Request = new XMLHttpRequest (); Gets the object.
Initreq (Reqtype,url,asynch);
else if (window. ActiveXObject) {//If it is IE browser
Request=new ActiveXObject ("msxml2.xmlhttp");
if (! Request) {
Request=new ActiveXObject ("Microsoft.XMLHTTP");
}
if (request) {
Initreq (Reqtype,url,asynch);
/* Unlikely to branch here, as IE users would be able to use either one of the
constructors*/
} else {
Alert ("Your browser does not permit the use" +
"Of all" application ' s features! ");}
} else {
Alert ("Your browser does not permit the use" +
"Of all" application ' s features! ");}
}
It should be noted that the Handleresponse function is the core of event handling.
<