Three methods to get XMLHttpRequest object_javascript skills

Source: Internet
Author: User
This article mainly introduces three methods to obtain the XMLHttpRequest object. For more information about how to obtain the XmlHttpRequest object, see

The Code is as follows:


// 1
Function getXMLHttpRequest (){
Var xmlHttpReq;
Try {// Firefox, Opera 8.0 +, Safari
XmlHttpReq = new XMLHttpRequest ();
} Catch (e ){
Try {// Internet Explorer
XmlHttpReq = new ActiveXObject ("Msxml2.XMLHTTP ");
} Catch (e ){
Try {
XmlHttpReq = new ActiveXObject ("Microsoft. XMLHTTP ");
} Catch (e ){
}
}
}
Return xmlHttpReq;
}

// 2
Function getXMLHttpRequest (){
Var xmlHttpReq = null;
If (window. ActiveXObject) {// Internet Explorer
XmlHttpReq = new ActiveXObject ("MSXML2.XMLHTTP. 3.0 ");
} Else if (window. XMLHttpRequest ){
XmlHttpReq = new XMLHttpRequest ();
}
Return xmlHttpReq;
}

// 3
Function getXMLHttpRequest (){
Var xmlHttpReq = null;
If (window. XMLHttpRequest) {// Mozilla Firefox, Opera 8.0 +, Safari
XmlHttpReq = new XMLHttpRequest ();
} Else {
If (window. ActiveXObject) {// Internet Explorer
Try {
XmlHttpReq = new ActiveXObject ("Microsoft. XMLHTTP ");
} Catch (e ){
Try {// Internet Explorer
XmlHttpReq = new ActiveXObject ("Msxml2.XMLHTTP ");
} Catch (e ){
}
}
}
}
Return xmlHttpReq;
}

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.