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

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.