views:

206

answers:

2

I have this code, which is working fine in FireFox, chrome and IE8 but is it not working on IE6 and IE7.

function GetXmlHttpObject() {
  //var xmlHttp = null;
  try {
    xmlHttp = new XMLHttpRequest();
  } catch (e) {
    try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  return xmlHttp;
}

function login()
{
    alert("Entered Login()"); 
    var url="http://server.com/ALUauth.php";
    xmlhttp.onreadystatechange=statechangedLogin;
    xmlhttp.open("GET", url, true);
    xmlhttp.send(null);
}

function statechangedLogin()
{
    if(xmlhttp.readyState==4)
    {
     alert("Entered State Changed Login");
     if (xmlhttp.responseText=="Please <a href=http://server.com/ALUauth.php?login&gt;login&lt;/a&gt;")
     {
      document.getElementById("ALUauth").innerHTML=xmlhttp.responseText;
     }
     else
     {
      GetEmailId();
     }

    }
}

function GetEmailId()
{   
    alert("Entered GetEmailId()");
    var url="http://server.com/GetPostEmail.php";
    url=url+"&sid="+Math.random();
    xmlhttp.onreadystatechange=statechangedLogin2;
    xmlhttp.open("GET", url, true);
    xmlhttp.send(null);
}

function statechangedLogin2()
{ 
    if(xmlhttp.readyState==4)
    {
        alert("Enter State Changed Login 2");
        if(xmlhttp.responseText=="Login again")
        {
                window.location="http://server.com/profile.html";
        }
    }
}

When I run the code in any other browser except for IE6 and 7 the output shows me all the alert boxes starting from: - Entered Login() - Entered State Changed Login - Entered GetEmailId() - Enter State Changed Login 2

and then the window location changes to http://server.com/profile.html

but when I run the same thing on IE 6 or 7, the code does not go into the statechangedLogin2(), and so the only alerts I get here are:

  • Entered Login()
  • Entered State Changed Login
  • Entered GetEmailId()

I am unable to figure out why this issue is occuring. Why is it happening, and what should I change? The project is working absolutely fine on other browsers include IE8.

Can some one help me figure this issue of mine.

+2  A: 

It's hard to read your code since it isn't formatted but I think your problem is that you're turning xmlhttp into a global variable rather than passing it around (I think...).

Try changing your xmlhttp.onreadystatechange to read

xmlhttp.onreadystatechange = function() { statechangedLogin(xmlhttp); };

and

xmlhttp.onreadystatechange = function() { statechangedLogin2(xmlhttp); };

and then change your functions for statechangedLogin and Login2 to accept the parameter

function statechangedLogin(xmlhttp) {
  // ... code here
}

The problem could be that you're accidentally overwriting the current request in the xmlhttp variable before it's finished which could cause those events not to fire. By making xmlhttp a local variable per function you can call those functions multiple times and they won't overwrite or stop each other.

Joshua
no this solution did not work.
Zeeshan Rang
and yes my xmlhttp is a global variable. do you think thats a problem for the code not to work in IE6 and 7?
Zeeshan Rang
This is just a shot in the dark but try changing the name of your second method and get rid of the number. I honestly don't know if a number is valid javascript in the IE6/7 timeframe. (e.g. change it to statechangedLoginTwo();
Joshua
I did that too.. i dont have any number in my function.. still not working. also made the xmlhttp local. and pass it on jsut the way you told in your answer.. its not working.
Zeeshan Rang
Can you update the original question with the latest code that you have tried and still doesn't work. Also be sure to update what you have tried. Someone else (or myself) may be able to figure it out with a little more information.
Joshua
A: 

Hello i am sorry to post my question again. but i was not getting any solution there so i tried to do this. thou i am sorry about it.

Anyways i was able to solve the situation with the help of my boss.

all i did was to give GetEmailId function its own session. something like this:

function GetEmailId()   
 { 
 alert("Entered GetEmailId()"); 
 xmlhttpTwo=GetXmlHttpObject();
 var url="http://server.com/GetPostEmail.php";
 url=url+"&sid="+Math.random();
 xmlhttpTwo.onreadystatechange=statechangedLogin2;
 xmlhttpTwo.open("GET", url, true);
 xmlhttpTwo.send(null);
 }

I tried this and it works absolutely fine on IE 6 n 7 ... :)

Best Zeeshan

Zeeshan Rang