0

My Ajax code works fine for Chrome but it doesn't give me anything when i try to run it using other browsers like Mozilla , IE7 opera. I am uploading my code here please tell me where is problem

function ajaxFunction(str){
    var ajaxRequest;  // The variable that makes Ajax possible!
    alert("in ajax");
    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
        ajaxRequest.onreadystatechange = function(){
        if((ajaxRequest.readyState == 4)){


                          var msg=ajaxRequest.responseText
                          var fld=document.getElementById("prtCnt");
                            alert('"' + msg + '"');
                          msg = msg.trim();
                          if(msg == "not")

                        {
                            var msg="This User Name is already taken !!!!";
                            fld.className="bp_invalid";
             //   fld.style.color=green;
                            fld.innerHTML=msg;
                        }
                        else if(msg == "yes")
                            {
                             var msg="This User Name is available !!!!";
                             fld.className="bp_valid";
             //   fld.style.color=green;
                            fld.innerHTML=msg;
                            }
                        //document.myForm.time.value = "";
            //document.myForm.time.value = ajaxRequest.responseText;
        }
    }
        var fld =document.getElementById(user);
        var url="loadjsp.jsp";
        url=url+"?user="+str;
    ajaxRequest.open("GET",url, true);
    ajaxRequest.send(null); 

}

Please tell me if anybody. I am new to ajax. thanx

mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
adesh
  • 832
  • 4
  • 14
  • 23

2 Answers2

1

One thing that might make IE fail is the use of trim as a string method. If you don't have any library that adds support for trim then on IE that line will produce and exception. Try removing the trim method to see if at least something is shown on screen.

Other browsers, at least on their latest versions, support the trim method, so if that's the case you still have to figure out why the rest of the browsers are failing.

  • I hav a sample code.... same as above . and Its working fine with all browsers but when i integrated it with ma project its not working? – adesh Mar 04 '12 at 05:00
  • The above code has some missing parts. For example, the _user_ variable is not defined in that snippet (but used to get the _fld_ element). Maybe the error is from other part of the code? As stated on other comments, the console log would be quite helpful too. – Germán Enríquez Mar 04 '12 at 05:06
  • Which library i need to add so that IE will also suport trim method . – adesh Mar 04 '12 at 05:42
  • No library is really needed, as it is a really simple piece of code. Look at the accepted answer in this thread, as it provides how to add trim support for browsers that do not implement it: http://stackoverflow.com/questions/2308134/trim-in-javascript-not-working-in-ie – Germán Enríquez Mar 04 '12 at 05:56
0

You have a missing ";" in the line

    var msg=ajaxRequest.responseText 

that might be causing the problem on some browsers

coseguera
  • 116
  • 3