Analysis of Ajax onreadystatechange support in Firefox

Source: Internet
Author: User

I. Problems: CopyCode The Code is as follows: var XMLHTTP;
Function savecarttodata (){
Createxmlhttprequest ();
VaR rndcode = new date (). gettime ();
VaR carturl = "A. asp? Cache = "+ rndcode
XMLHTTP. onreadystatechange = function (){
.....

}
XMLHTTP. Open ("get", carturl, true );
XMLHTTP. Send (null );
}

The code above, XMLHTTP. onreadystatechange = function () {...}; can be executed in FF, but if it is changed

XMLHTTP. Open ("get", URL, false); the problem cannot be solved.

Cause analysis:

First, XMLHTTP. Send () cannot be used. The content is required. If there is no content, use null.

Second: after testing, we found that onreadystatechange works normally in IE, but in ff3, we can only run the code when readystate is set to 0. You cannot run the Code with readystate = 4. You can find a reason on the network:
In the XMLHttpRequest. differences between the onreadystatechange Method: In ff, when the status is 1 (that is, when XMLHttpRequest has called open but has not called send), FF will continue to execute the code after onreadystatechange, after the code is executed, the code in the status of onreadystatechange is 2, 3, and 4, while ie waits for the status 2 to arrive. After the code in the status of 2, 3, and 4 in onreadystatechange is executed, continue to execute the following code. This problem occurs. We often use the onreadystatechange code to process the data obtained from the server (this data is only when the onreadystatechange status is 4, so there is no problem in IE, because it will wait for the arrival of onreadystatechange status 4, the data after onreadystatechange is executed, but because FF will not wait until onready After statechange status 4 arrives, the code after onreadystatechange is executed. Therefore, the code after statechange cannot process the data obtained from the server. What should I do?

Solution: use JavaScript closures (this solution is inspired by GMAP ). We pass a function to onreadystatechange to process the data returned from the server. But onreadystatechange is a non-parameter function. What should we do? The method has been introduced in the previous method for passing parameters in Javascript attachevent. Here we will introduce it a little bit, that is, passing a parameter to onreadystatechange, but using return in onreadystatechange is a no-argument function, you can use this input parameter in this no-argument function. This method can run normally in IE and FF, so this is a good method.

The use of closures is complicated. In addition, onload is used for FF on the Internet. After troubleshooting, the reason mentioned in the summary above is fundamental. That is to say, in FF, after onreadystatechange is executed for the first time, it continues to send, but will not go back to onreadystatechange, it's always silly to go on.

I directly changed it:Copy codeThe Code is as follows: XMLHTTP. onreadystatechange = xmlhandle;
XMLHTTP. Open ("get", URL, false );
XMLHTTP. Send (null );
XMLHTTP. onreadystatechange = xmlhandle; // Add a line here to block ff and let it do it again.

Function xmlhandle (){
If (XMLHTTP. readystate <4 ){
......
} Else if (XMLHTTP. readystate = 4 & XMLHTTP. Status = 200 ){
VaR cartresult = Number (XMLHTTP. responsetext );
If (cartresult = 1 ){
Window. Location. href = 'a. asp ';
} Else if (cartresult = 2 ){
......;
} Else {
Window. Location. href = '/';
}
}
}

But this does not work. The original ff 3 is changed to XMLHTTP. onreadystatechange = xmlhandle (); however, with parentheses added, ie does not work. Alas, FF is a chicken skin. Now, I feel that FF is a pure "Standard Support" title, but it is a waste of work.ProgramPersonnel Time garbage. However, this program is really important, and there is no way to debug it again to see if there is a simpler way, as shown below:Copy codeThe Code is as follows: XMLHTTP. Open ("get", URL, false );
XMLHTTP. Send (null );
If (XMLHTTP. Status = 200)
Xmlhandle ();

This code can be used in IE and ff. However, because it is a synchronous call, you need to prompt before the readystate <4 does not get the result, which is very friendly for customers with slow network speeds. However, to obtain such a response in the local machine, because the local machine is fast, it will cause no prompt to the customer, so this code is not needed for the moment.

only browser type analysis is added. copy Code the code is as follows: function getos ()
{< br> var osobject = "";
If (navigator. useragent. indexof ("MSIE")> 0) {
return "MSIE"; // IE browser
}< br> If (isfirefox = navigator. useragent. indexof ("Firefox")> 0) {
return "Firefox"; // Firefox browser
}< br> If (issafari = navigator. useragent. indexof ("safari")> 0) {
return "safari"; // safan browser
}< br> If (iscamino = navigator. useragent. indexof ("Camino")> 0) {
return "Camino"; // Camino browser
}< br> If (ismozilla = navigator. useragent. indexof ("Gecko/")> 0) {
return "gecko"; // gecko browser
}< BR >}

change the Ajax code to copy Code the code is as follows: var rndcode = new date (). gettime ();
var carturl = ". asp? Cache = "+ rndcode
var btype = getos ();
XMLHTTP. onreadystatechange = (btype! = "Firefox ")? Xmlhandle (): xmlhandle;
XMLHTTP. open ("get", carturl, false);
XMLHTTP. send (null);
XMLHTTP. onreadystatechange = (btype! = "Firefox ")? Xmlhandle (): xmlhandle;

Example 2 copy Code the code is as follows: // gets the type of the browser. To solve the problem of onreadystatechange incompatibility
function getos ()
{< br> var osobject = "";
If (navigator. useragent. indexof ("MSIE")> 0) {
return "MSIE"; // IE browser
}< br> If (isfirefox = navigator. useragent. indexof ("Firefox")> 0) {
return "Firefox"; // Firefox browser
}< br> If (issafari = navigator. useragent. inde XOF ("safari")> 0) {
return "safari"; // safan browser
}< br> If (iscamino = navigator. useragent. indexof ("Camino")> 0) {
return "Camino"; // Camino browser
}< br> If (ismozilla = navigator. useragent. indexof ("Gecko/")> 0) {
return "gecko"; // gecko browser
}< BR >}< br> var objhttp;
function searchcommoditybygroupid (groupid)
{< br> objhttp = gethttprequest ();
var TT = new date ();
var url = "getcom Moditylistbygroupid.htm? Commoditygroupid = "+ groupid +" & time = "+ tt;
var btype = getos ();

Objhttp. onreadystatechange = (btype = "Firefox ")? Getcommodity (): getcommodity;
Objhttp. Open ("get", URL, false );
Objhttp. Send (null );
Objhttp. onreadystatechange = (btype = "Firefox ")? Getcommodity (): getcommodity;
}
Function getcommodity (){

If (objhttp. readystate = 4)
{
If (objhttp. Status = 200)
{
Document. getelementbyid ("commoditydiv"). innerhtml = objhttp. responsetext;
}
}
}

Function gethttprequest (){
VaR httprequest;
If (window. XMLHttpRequest ){
Httprequest = new XMLHttpRequest ();
If (httprequest. overridemimetype ){
Httprequest. overridemimetype ('text/xml ');
}
} Else if (window. activexobject ){
Try {
Httprequest = new activexobject ("msxml2.xmlhttp ");
} Catch (e ){
Try {
Httprequest = new activexobject ("Microsoft. XMLHTTP ");
} Catch (e ){}
}
}
Return httprequest;
}

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.