...

View Full Version : Problems in IE (AJAX Code)



Osiris
12-16-2005, 10:49 PM
Ok, so as you can tell, I'm having some problems with some AJAX code of mine. The code works fine in FF and Opera, but not at all in IE. Could someone possibly tell me why?


var xmlDoc;
var chatContent = "";
var timeout;

function loadXML() {
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
xmlDoc = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
xmlDoc = false;
}
}
@end @*/
if (!xmlDoc && typeof XMLHttpRequest != 'undefined') {
xmlDoc = new XMLHttpRequest();
}

if(!xmlDoc) {
alert('Your browser cannot handle this script');
return;
}

if(xmlDoc.overrideMimeType) {
xmlDoc.overrideMimeType('text/xml');
}
xmlDoc.onreadystatechange = function () {
if(xmlDoc.readyState == 4) {
if(xmlDoc.status == 200) {
var xmlContent = xmlDoc.responseXML;

for(var x = 0; x < xmlContent.getElementsByTagName("broadcast").length; x++) {
var author = xmlContent.getElementsByTagName("broadcast")[x].getAttribute("poster");
var author_id = xmlContent.getElementsByTagName("broadcast")[x].getAttribute("posterid");
var emote = xmlContent.getElementsByTagName("broadcast")[x].getAttribute("emote");
var message = xmlContent.getElementsByTagName("broadcast")[x].firstChild.data;
if(emote == 0) {
chatContent += '[Broadcast]<a href="/profile.php?uid='+author_id+'">'+author+'</a> - '+message+'<br />';
} else {
chatContent += '[Broadcast]<a href="/profile.php?uid='+author_id+'">'+author+'</a> '+message+'<br />';
}
}

for(var x = 0; x < xmlContent.getElementsByTagName("message").length; x++) {
var author = xmlContent.getElementsByTagName("message")[x].getAttribute("poster");
var author_id = xmlContent.getElementsByTagName("message")[x].getAttribute("posterid");
var emote = xmlContent.getElementsByTagName("message")[x].getAttribute("emote");
var message = xmlContent.getElementsByTagName("message")[x].firstChild.data;
if(emote == 0) {
chatContent += '<a href="/profile.php?uid='+author_id+'">'+author+'</a> - '+message+'<br />';
} else {
chatContent += '<a href="/profile.php?uid='+author_id+'">'+author+'</a> '+message+'<br />';
}
}

document.getElementById("chatDiv").innerHTML = chatContent;
chatContent = "";
timeout = setTimeout('loadXML()', 5000);
} else {
document.getElementById("chatDiv").innerHTML = "There was an error with the chat script. Please report this to an admin.";
}
}
}

xmlDoc.open('GET', 'http://somewhere.com/xml_file.php', true);
xmlDoc.send(null);;
}


Thanks!

Basscyst
12-17-2005, 12:43 AM
What exactly are you experiencing?

Osiris
12-17-2005, 01:41 AM
What exactly are you experiencing?
Well, it's supposed to grab the XML from the page and display it in an iframe as chat, and in Firefox and Opera, it works, but it doesn't in IE. And I have no clue how to fix it, since my IE for some reason keeps changing it back whenever I try to enable script errors.

EvilPenguin
12-17-2005, 01:51 AM
I have had some problems with using AJAX in IE, as IE caches the returned document if the request is the same. The trick is either to set HTTP headers on the xml document for no cacheing (instructions below if you're using PHP), or (ugly and hackish) append a GET request with a random value. I don't know if this is the problem you're having though.


<?php
// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

// always modified
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");

// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);

// HTTP/1.0
header("Pragma: no-cache");
?>

Osiris
12-17-2005, 01:59 AM
Nope, no good. If you'd like a link to the site in question, PM me and I'll give it to you. I would post it here, but I'm A: worried about spam, and B: worried about someone exploiting flaws in un-finished coding, so...

Osiris

Osiris
12-17-2005, 08:09 AM
Alrighty, it's all good. I got it fixed... I ended up having to fork the code off to have the IE code use the Microsoft.XMLDOM object instead of the Microsoft.XMLHTTP object. A bit of a pain, but meh - it works. Thanks for the help, everyone!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum