Do you still remember the nasty Browser Wars a few years ago? Nothing returns the same result on different browsers. Whether you believe it or not, these wars are still going on, although the scale is small. But it is strange that XMLHttpRequest has become one of the victims of the war. Therefore, you may need to use different methods to obtain the XMLHTTPRequest object. I will explain it in detail below.
Use the Microsoft browser
Microsoft Internet Explorer uses the MSXML parser to process XML. Therefore, if you write an Ajax applicationProgramTo deal with Internet Explorer, you must create an object in a special way.
But it is not that simple. Depending on the version of JavaScript technology installed in Internet Explorer, MSXML actually has two different versions. Therefore, you must writeCode. See the following Code. The Code creates an XMLHttpRequest on the Microsoft browser.
VaR XMLHTTP = false; Try { XMLHTTP = new activexobject ("msxml2.xmlhttp "); } Catch (e ){ Try { XMLHTTP = new activexobject ("Microsoft. XMLHTTP "); } Catch (E2 ){ XMLHTTP = false; } } |
Now you only need to remember the two lines of code:
XMLHTTP = new activexobject ("msxml2.xmlhttp ");
And
XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
These two lines of code basically try to create an object using a version of MSXML. If the Code fails, use another version to create the object. Good, right? If none of them are successful, set the XMLHTTP variable to false to tell you that the Code has a problem. If this happens, it may be because a non-Microsoft browser is installed and different code is required.
Processing Mozilla and non-Microsoft browsers
If the selected browser is not Internet Explorer, or you want to write code for a non-Microsoft browser, you need to use different codes. In fact, the simple code is as follows:
<Script language = "JavaScript" type = "text/JavaScript"> VaR XMLHTTP = new XMLHttpRequest (); </SCRIPT> |
This line of much simpler Code creates XMLHttpRequest objects in Mozilla, Firefox, Safari, opera, and basically all non-Microsoft browsers that support Ajax in any form or method.
Combined
The key is to support all browsers. Who wants to write an application that can only be used in Internet Explorer or non-Microsoft browsers? Or worse, write an application twice? Of course not! Therefore, the Code must support both Internet Explorer and non-Microsoft browsers. The following code is displayed.
<Script language = "JavaScript" type = "text/JavaScript"> VaR XMLHTTP = false;Try { XMLHTTP = new activexobject ("msxml2.xmlhttp "); } Catch (e ){ Try { XMLHTTP = new activexobject ("Microsoft. XMLHTTP "); } Catch (E2 ){ XMLHTTP = false; } } If (! XMLHTTP & typeof XMLHttpRequest! = 'Undefined '){ XMLHTTP = new XMLHttpRequest (); } </SCRIPT> |
The core of this Code is divided into three steps:
1. Create a variable XMLHTTP to reference the XMLHTTPRequest object to be created.
2. Try to create this object in Microsoft browser:
Try to create it using the msxml2.xmlhttp object.
If it fails, try the Microsoft. XMLHTTP object again.
3. If XMLHTTP is still not created, this object is created in non-Microsoft mode.
at last, XMLHTTP should reference a valid XMLHTTPRequest object, regardless of the browser that runs.