PDA

View Full Version : Ajax works in firefox but not IE 6


usik
08-14-2007, 02:57 AM
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

<!--
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!

A1ien51
08-14-2007, 04:14 AM
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

rwedge
08-14-2007, 04:51 AM
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.
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 errorsfunction 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;
}
}
}

usik
08-14-2007, 08:36 AM
Thanks guys, will try your solution rwedge

usik
08-14-2007, 10:31 AM
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

A1ien51
08-14-2007, 02:09 PM
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

usik
08-15-2007, 11:57 AM
Thanks guys I got it working, special thanks to Eric :)