Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-14-2007, 02:57 AM   PM User | #1
usik
New Coder

 
Join Date: Aug 2007
Location: Wagga, Australia
Posts: 59
Thanks: 0
Thanked 1 Time in 1 Post
usik is on a distinguished road
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!
usik is offline   Reply With Quote
Old 08-14-2007, 04:14 AM   PM User | #2
A1ien51
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
A1ien51 will become famous soon enough
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]
A1ien51 is offline   Reply With Quote
Old 08-14-2007, 04:51 AM   PM User | #3
rwedge
Regular Coder

 
Join Date: Feb 2005
Posts: 679
Thanks: 0
Thanked 16 Times in 15 Posts
rwedge is on a distinguished road
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
rwedge is offline   Reply With Quote
Old 08-14-2007, 08:36 AM   PM User | #4
usik
New Coder

 
Join Date: Aug 2007
Location: Wagga, Australia
Posts: 59
Thanks: 0
Thanked 1 Time in 1 Post
usik is on a distinguished road
Thanks guys, will try your solution rwedge
usik is offline   Reply With Quote
Old 08-14-2007, 10:31 AM   PM User | #5
usik
New Coder

 
Join Date: Aug 2007
Location: Wagga, Australia
Posts: 59
Thanks: 0
Thanked 1 Time in 1 Post
usik is on a distinguished road
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
usik is offline   Reply With Quote
Old 08-14-2007, 02:09 PM   PM User | #6
A1ien51
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
A1ien51 will become famous soon enough
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]
A1ien51 is offline   Reply With Quote
Old 08-15-2007, 11:57 AM   PM User | #7
usik
New Coder

 
Join Date: Aug 2007
Location: Wagga, Australia
Posts: 59
Thanks: 0
Thanked 1 Time in 1 Post
usik is on a distinguished road
Thanks guys I got it working, special thanks to Eric
usik is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:46 AM.


Advertisement
Log in to turn off these ads.