Another way of thinking: Solve the Problem of incompatibility between Ajax programs and IE7

Source: Internet
Author: User
Problem proposal

Recently planned to restructure the Web technology site, a small test, with PHP + xajax to achieve a simple function of small page (http://www.netpu.net/test/test.php ). I never thought that this page has been normal for firfox testing, but it is normal for IE7 testing, sometimes error. Turning on the debug function of xajax fails to obtain valuable information, which is very depressing.

So I used Ajax + IE7 as the keyword to search in the search engine and found a lot about Ajax.CodeI ran the wrong post in IE7, but no one answered the question, so I had to solve it myself.

Solution

Since this page runs normally on both Firefox and IE6, let's take a look at the differences between Firefox, IE6, and IE7 when processing Ajax code?

We know that AJAX applicationsProgramXMLHttpRequest is the core, but the method for creating this object is quite different because of compatibility issues with different browsers.

On Mozilla, Firefox, Safari, opera, and other browsers, use the following statement to create XMLHttpRequest:

Code:[Copy to clipboard]

VaR XMLHTTP = new XMLHttpRequest ();

In IE6 and earlier versions, use the following statement to create XMLHttpRequest:

Code:[Copy to clipboard]

XMLHTTP = new activexobject ("msxml2.xmlhttp"); or
XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");

What is the difference between IE7 and IE6?
On msdn: about Native XMLHttp:
Http://msdn2.microsoft.com/en-us/library/ms537505.aspx

In Internet Explorer 6 and earlier, XMLHTTP was implemented as an ActiveX object provided by Microsoft XML (MSXML). Beginning with Internet Explorer 7, XMLHTTP is also exposed as a native scripting object.
That is to say, IE7 adds Native XMLHttp Support.

At the same time, an example code is provided:

code: [copy to clipboard]

var XMLHTTP = NULL;
If (window. XMLHttpRequest ){
// If IE7, Mozilla, Safari, and so on: use native object.
XMLHTTP = new XMLHttpRequest ();
}
else
{
If (window. activexobject ){
//... otherwise, use the ActiveX Control for ie5.x and ie6.
XMLHTTP = new activexobject ('msxml2. xmlhttp.3.0 ');
}
}

It can be seen that IE7 has more Native XMLHttp Support than IE6. Will it be a problem caused by Native XMLHttp? What if Native XMLHttp is not used? Fortunately, IE7 designers have taken this into consideration. IE7 provides two functions:
One is to disable "Native XMLHttp Support" Through settings (enabled by default ).

Disable the Enable Native XMLHttp Support check box.

Another feature is compatible with IE6's method for creating XMLHttpRequest.

Solution

OK. In this case, we disable "Native XMLHttp Support" to see if our program is normal?
Test results, our program is all normal.

But wait, we cannot require users to disable "Native XMLHttp Support" before accessing our page? Sweat!

So we can only find a way to create code in XMLHttpRequest of Ajax.
What can I do? To be honest, I am a super layman for Ajax. I can't help but stick to it. Since the problem lies in creating an XMLHTTPRequest object, let's start from here.

ManyArticleBoth stressed that to ensure cross-browser compatibility, first check whether Native XMLHttp is supported.
For example, this article: Ajax on IE 7: Check native first
Http://ajaxian.com/archives/ajax-on-ie-7-check-native-first

In addition, the example code of creating XMLHttpRequest in IE7 mentioned above is checked first to check whether Native XMLHttp is supported. Let's see how xajax is used to create an object.

Code:[Copy to clipboard]

/*
Function: getrequestobject

Construct an XMLHTTPRequest object dependent on the capabilities
Of the browser.

Returns:

Object-JavaScript xhr object.
*/
If ("undefined "! = Typeof XMLHttpRequest ){
Xajax. Tools. getrequestobject = function (){
Return new XMLHttpRequest ();
}
} Else if ("undefined "! = Typeof activexobject ){
Xajax. Tools. getrequestobject = function (){
Try {
Return new activexobject ("msxml2.xmlhttp. 4.0 ");
} Catch (e ){
Xajax. Tools. getrequestobject = function (){
Try {
Return new activexobject ("msxml2.xmlhttp ");
} Catch (E2 ){
Xajax. Tools. getrequestobject = function (){
Return new activexobject ("Microsoft. XMLHTTP ");
}
Return xajax. Tools. getrequestobject ();
}
}
Return xajax. Tools. getrequestobject ();
}
}
} Else if (window. createrequest ){
Xajax. Tools. getrequestobject = function (){
Return window. createrequest ();
}
} Else {
Xajax. Tools. getrequestobject = function (){
Throw {Name: 'getrequestobject', message: 'xmlhttprequest is not available, xajax is disabled '}
}
}

We can't predict whether Native XMLHttp is supported first. In this way, once IE7 is used, the Native XMLHttp of IE7 will be enabled and our program will go wrong.

In another way, what if we first check the ActiveX object? Let's just do it. We modified this part of the code (in simple words, we changed the order) and tested that everything was normal, firefox, IE6, and IE7 are normal (sorry, I only have these browsers.

This is a success! What is the modified Code? This is a simple exercise. Or you can go to the Web Technology Forum (http://bbs.netpu.net) for help.

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.