Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Aug 2007
    Location
    Wagga, Australia
    Posts
    59
    Thanks
    0
    Thanked 1 Time in 1 Post

    Ajax works in firefox but not IE 6

    hi guys,
    I've only recently started to use ajax and the script in question works perfectly in mozilla firefox but not in IE 6?? IE doesn't even state that there is an error?
    here is my code
    Code:
    <!--
    var fromUser;
    var toUser;
    function scroll()
    {
    var objDiv = document.getElementById("messages");
    objDiv.scrollTop = objDiv.scrollHeight;
    }
    
    function submitenter(myfield,e)
    {
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;
    
    if (keycode == 13)
       {
       messageSend(document.getElementById("message").value);
       document.getElementById("message").value = "";
       return false;
       }
    else
       return true;
    }
    
    function createRequestObject() {
    var xmlHttp;
      try
       {
         // Firefox, Opera 8.0+, Safari
         xmlHttp=new XMLHttpRequest();
       }
      catch (e)
       {
         // Internet Explorer
       try
        {
          xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
       catch (e)
        {
          xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
       }
      return xmlHttp;
    }
    
    var http = createRequestObject();
    
    function messageFetch() {
        http.open('get', 'ajax/messages.php?from='+fromUser+'&to='+toUser, true);
        http.onreadystatechange = handleResponse;
        http.send(null);
    }
    
    function handleResponse() {
        if(http.readyState == 4){
            var response = http.responseText;
            if(response.length != 0 && response != null) {
              document.getElementById('messages').innerHTML = document.getElementById('messages').innerHTML + response;
              soundManager.play('beep');
            }
        }
    }
    
    function messageSend(message) {
        http.open('get', 'ajax/sendmessage.php?message='+message+'&from='+fromUser+'&to='+toUser, true);
        document.getElementById('messages').innerHTML = document.getElementById('messages').innerHTML + "From you:<br />" + message + "<br />" ;
        http.send(null);
        setTimeout("scroll()", 50);
    }
    
    function setTimer() {
      messageFetch();
      setTimeout("scroll()", 50);
      getMessages();
    }
    
    function getMessages() {
      setTimeout("setTimer()", 5000);
    }
    //-->
    thanks in advance!

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Have you tried to debug it to see if it is making it to the server? Have you added alert statemenets or used Firebug Lite to figure out where it is stopping?

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    You are trying to reuse the inital http object which is set on page load. It will work once with IE.

    You will have to re-initalize the XMLHttpRequest for each use for IE.
    Code:
    var isIE = (document.all && !window.opera)? true:false;
    function messageFetch() {
     if (isIE) http = createRequestObject();
        http.open('get', 'ajax/messages.php', true);
        http.onreadystatechange = handleResponse;
        http.send(null);
    }
    
    function messageSend(message) {
     if (isIE) http = createRequestObject();
        http.open('get', 'ajax/sendmessage.php?message='+escape(message)+'&from='+escape(fromUser)+'&to='+escape(toUser), true);
        document.getElementById('messages').innerHTML = document.getElementById('messages').innerHTML + "From you:<br />" + message + "<br />" ;
        http.send(null);
        setTimeout("scroll()", 50);
    }
    You should check the status better to see any errors
    Code:
    function handleResponse() {
        if(http.readyState == 4){
            if (http.status  == 200) {
               var response = http.responseText;
                if(response.length != 0 && response != null) {
                  document.getElementById('messages').innerHTML = document.getElementById('messages').innerHTML + response;
                  soundManager.play('beep');
                }
            } else {
                  alert('Error Message = '+http.status+' : '+http.statusText);
                  return;
            }
        }
    }
    Last edited by rwedge; 08-14-2007 at 08:16 AM. Reason: focus

  • #4
    New Coder
    Join Date
    Aug 2007
    Location
    Wagga, Australia
    Posts
    59
    Thanks
    0
    Thanked 1 Time in 1 Post
    Thanks guys, will try your solution rwedge

  • #5
    New Coder
    Join Date
    Aug 2007
    Location
    Wagga, Australia
    Posts
    59
    Thanks
    0
    Thanked 1 Time in 1 Post
    I have run into more problems, it works the 1st time round then it just keeps looping the 1st data received and doesn't receive any more, atleast it can send messages :P

  • #6
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    You are cached, hapens when you use GET.

    Either change to a post or add this after your open statement

    http.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #7
    New Coder
    Join Date
    Aug 2007
    Location
    Wagga, Australia
    Posts
    59
    Thanks
    0
    Thanked 1 Time in 1 Post
    Thanks guys I got it working, special thanks to Eric


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •