How to Create ajax objects and create multiple ajax objects compatible with multiple browsers

Source: Internet
Author: User

How to Create ajax objects and create multiple ajax objects compatible with multiple browsers

This article describes how to Create ajax objects and make them compatible with multiple browsers. For more information, see

<Script> function createAjax () {var request = false; // XMLHttpRequest in the window object is non-IE, including (IE7 or later) if (window. XMLHttpRequest) {request = new XMLHttpRequest (); if (request. overrideMimeType) {request. overrideMimeType ("text/xml");} // The ActiveXObject attribute in the window object is IE} else if (window. activeXObject) {var versions = ['Microsoft. XMLHTTP ', 'msxml. XMLHTTP ', 'msxml2. XMLHTTP.7.0 ', 'msxml2. XMLHTTP.6.0 ', 'msxml2. XMLHTTP.5.0 ', 'msxml2. XMLHTTP.4.0 ', 'msxml2. XMLHTTP.3.0 ', 'msxml2. XMLHTTP ']; for (var I = 0; I <versions. length; I ++) {try {request = new ActiveXObject (versions [I]); if (request) {return request ;}} catch (e) {request = false ;}}return request;} var ajax = createAjax (); alert (ajax); </script>

How to create a common AJAX object

Determine whether the server supports ajax
Function tel (){
Var tel = false;
Try {
Tel = new XMLHttpRequest ();
} Catch (e ){
// For IE browsers
Try {
Tel = new ActiveXObject ('msxml2. xmlhttp'); // ie8
} Catch (e ){
Try {
Tel = new ActiveXObject ('Microsoft. xmlhttp ');
} Catch (e ){
Alert ('your browser does not support ajax ');
}
}
}
Return tel;
}
Method 2:
Function tel (){
Var tel = false;
If (window. XMLHttpRequest ){
Tel = new XMLHttpRequest ();
} Else {
Tel = new window. ActiveXObject ('Microsoft. xmlhttp ');
}
Return tel;
}

Browser compatibility issues of the js ajax setrequestheader method

How is xmlhttp created?
The xmlhttp object you created in Firefox is unsuccessful. Only create in IE
In IE and Firefox, the method for creating the xmlhttp object is different. Now you can use this method to create this object. You can try it:
Function createxmlhttp () {// create an xmlhttp object
Var xmlhttp = false;
Try {
Xmlhttp = new ActiveXObject ("Msxml2.XMLHTTP ");
} Catch (e ){
Try {
Xmlhttp = new ActiveXObject ("Microsoft. XMLHTTP ");
} Catch (e ){
Xmlhttp = false;
}
}
If (! Xmlhttp & typeof XMLHttpRequest! = 'Undefined '){
Xmlhttp = new XMLHttpRequest ();
If (xmlhttp. overrideMimeType ){
Xmlhttp. overrideMimeType ('text/html '); // you can specify the MiME type.
}
}
Return xmlhttp;
}

Then, call the following in your AJAX method:
Var xmlhttp = createxmlhttp ();
If (! Xmlhttp ){
Alert ("your browser does not support XMLHTTP !! ");
Return;
}
Xmlhttp. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded ");

The above is my AJAX implementation method. After testing, Firefox/IE and other browsers are easy to use.
Question asked

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.