AJAX Hacks the HACK1. to detect browser compatibility

Source: Internet
Author: User
Tags object bool http request window client
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.

<

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.