开发者

AJAX issue, works in all browsers except IE

This code works in Chrome and Firefox, but not IE (which is to be expected). Does anyone see anything wrong with this code that would render it useless in IE?

var waittime=400;
chatmsg = document.getElementById("chatmsg");
room = document.getElementById("roomid").value; 
sessid = document.getElementById("sessid").value;
chatmsg.focus()
document.getElementById("chatwindow").innerHTML = "loading...";
document.getElementById("userwindow").innerHTML = "Loading User List...";
var xmlhttp = false;
var xmlhttp2 = false;
var xmlhttp3 = false;
function ajax_read() {
if(window.XMLHttpRequest){
    xmlhttp=new XMLHttpRequest();
    if(xmlhttp.overrideMimeType){
        xmlhttp.overrideMimeType('text/xml');
    }
} else if(window.ActiveXObject){
    try{
        xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
        try{
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        } catch(e){
        }
    }
}
if(!xmlhttp) {
    alert('Giving up :( Cannot create an XMLHTTP instance');
    return false;
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4) {
    document.getElementById("chatwindow").innerHTML = xmlhttp.responseText;
    setTimeout("ajax_read()", waittime);
    }
}
xmlhttp.open('GET','methods.php?method=r&room=' + room +'',true);
xmlhttp.send(null);
}
function user_read() {
if(window.XMLHttpRequest){
    xmlhttp3=new XMLHttpRequest();
    if(xmlhttp3.overrideMimeType){
        xmlhttp3.overrideMimeType('text/xml');
    }
} else if(window.ActiveXObject){
    try{
        xmlhttp3=new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
        try{
            xmlhttp3=new ActiveXObject("Microsoft.XMLHTTP");
        } catch(e){
        }
    }
}
if(!xmlhttp3) {
    alert('Giving up :( Cannot create an XMLHTTP instance');
    return false;
}
xmlhttp3.onreadystatechange = function() {
if (xmlhttp3.readyState==4) {
    document.getElementById("userwindow").innerHTML = xmlhttp3.responseText;
    setTimeout("user_read()", 10000);
    }
}
xmlhttp3.open('GET','methods.php?method=u&room=' + room +'',true);
xmlhttp3.send(null);
}
function ajax_write(url){
if(window.XMLHttpRequest){
    xm开发者_如何学Golhttp2=new XMLHttpRequest();
    if(xmlhttp2.overrideMimeType){
        xmlhttp2.overrideMimeType('text/xml');
    }
} else if(window.ActiveXObject){
    try{
        xmlhttp2=new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
        try{
            xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
        } catch(e){
        }
    }
}
if(!xmlhttp2) {
    alert('Giving up :( Cannot create an XMLHTTP instance');
    return false;
}
xmlhttp2.open('GET',url,true);
xmlhttp2.send(null);
}
function submit_msg(){
nick = document.getElementById("chatnick").value;
msg = document.getElementById("chatmsg").value;
document.getElementById("chatmsg").value = "";
ajax_write("methods.php?method=w&m=" + msg + "&n=" + nick + "&room=" + room +    "&sessid=" + sessid + "");
}
function keyup(arg1) { 
if (arg1 == 13) submit_msg(); 
}
var intUpdate = setTimeout("ajax_read()", waittime);
var intUpdate = setTimeout("user_read()", 0);


I'm going to avoid the question slightly and suggest you use jQuery =P. 90% of your code would evaporate. For example, your user_read function would become:

function user_read() {
  $.ajax({
     type: "GET",
     url: "methods.php",
     data: {method: "u", room: room}
     dataType: "html"
     success: function (data, status, xhr) {
         $("#userwindow").html(data);
         setTimeout(user_read, 10000);
     }
   });
}

Furthermore, jQuery is also designed to work on all browsers, so you shouldn't have IE problems any more.


Try run on IE some example using jQuery library - just for partial test that browser is 'ok' and code have bug.


jQuery would be best for multiple ajax requests. Else, there are quite a few problems that more advanced browsers automatically fix, while IE may fail on. A few missing semicolons. You also have 2 intUpdate declarations for 2 different intervals at the end. I've also noticed that "xmlhttp2" has no onreadystatechange function.

Using a checker like JSLint will help alot.

I did my best with your code. Try this, it should result in no errors:

var waittime = 400;
chatmsg = document.getElementById("chatmsg");
room = document.getElementById("roomid").value; 
sessid = document.getElementById("sessid").value;
chatmsg.focus();
document.getElementById("chatwindow").innerHTML = "loading...";
document.getElementById("userwindow").innerHTML = "Loading User List...";
var xmlhttp = false;
var xmlhttp2 = false;
var xmlhttp3 = false;

function ajax_read() {
    if(window.XMLHttpRequest){
    xmlhttp=new XMLHttpRequest();
    if(xmlhttp.overrideMimeType){
        xmlhttp.overrideMimeType('text/xml');
    }
} else if(window.ActiveXObject){
    try{
        xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
        try{
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        } catch(e){
    alert('Giving up :( Cannot create an XMLHTTP instance');
}
    }
}

xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4) {
    document.getElementById("chatwindow").innerHTML = xmlhttp.responseText;
    setTimeout(function(){ajax_read();}, waittime);
    }
};
xmlhttp.open('GET','methods.php?method=r&room=' + room +'',true);
xmlhttp.send();
}

function user_read() {
if(window.XMLHttpRequest){
    xmlhttp3=new XMLHttpRequest();
    if(xmlhttp3.overrideMimeType){
        xmlhttp3.overrideMimeType('text/xml');
    }
} else if(window.ActiveXObject){
    try{
        xmlhttp3=new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
        try{
            xmlhttp3=new ActiveXObject("Microsoft.XMLHTTP");
        } catch(e){
    alert('Giving up :( Cannot create an XMLHTTP instance');

        }
    }
}

xmlhttp3.onreadystatechange = function() {
if (xmlhttp3.readyState==4) {
    document.getElementById("userwindow").innerHTML = xmlhttp3.responseText;
    setTimeout(function(){user_read();}, 10000);
    }
};
xmlhttp3.open('GET','methods.php?method=u&room=' + room +'',true);
xmlhttp3.send();
}

function ajax_write(url){
if(window.XMLHttpRequest){
    xmlhttp2=new XMLHttpRequest();
    if(xmlhttp2.overrideMimeType){
        xmlhttp2.overrideMimeType('text/xml');
    }
} else if(window.ActiveXObject){
    try{
        xmlhttp2=new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
        try{
            xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
        } catch(e){
    alert('Giving up :( Cannot create an XMLHTTP instance');

        }
    }
}
xmlhttp2.open('GET',url,true);
xmlhttp2.send();
}

function submit_msg(){
nick = document.getElementById("chatnick").value;
msg = document.getElementById("chatmsg").value;
document.getElementById("chatmsg").value = "";
ajax_write("methods.php?method=w&m=" + msg + "&n=" + nick + "&room=" + room +    "&sessid=" + sessid + "");
}

function keyup(arg1) { 
if (arg1 == 13) {submit_msg();}
}

var intUpdate = setTimeout(function(){ajax_read();}, waittime);
user_read();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜