spenoir
04-20-2006, 11:26 AM
Anyone Know why my first ajax script doesn't work in IE. It works in Firefox and Opera. I've attached the xml, js and html in a zip.
Thanks in advance.:thumbsup:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script language="javascript">
var req=null;
var console=null;
var READY_STATE_UNINITIALIZED=0;
var READY_STATE_LOADING=1;
var READY_STATE_LOADED=2;
var READY_STATE_INTERACTIVE=3;
var READY_STATE_COMPLETE=4;
function initXMLHTTPRequest () {
var o;
try{
o = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
o = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){
o = false
}
}
if(!o && typeof XMLHttpRequest!='undefined'){
o = new XMLHttpRequest();
}
// o.async=false;
return o;
}
function sendRequest (url,params,HttpMethod) {
if (!HttpMethod) {
HttpMethod="GET";
}
req=initXMLHTTPRequest();
if (req) {
req.onreadystatechange=onReadyState;
req.open(HttpMethod,url,true);
req.setRequestHeader ("Content-Type", "text/xml");
req.send(params);
}
}
function onReadyState() {
var ready=req.readyState;
var data=null;
if (ready==READY_STATE_COMPLETE) {
data=req.responseText;
setDataXML(req);
}
else {
data="loading...["+ready+"]";
}
}
window.onload=function() {
document.getElementById('writeroot');
sendRequest("books.xml");
}
function setDataXML(req)
{
var books = req.responseXML.getElementsByTagName('book');
for (var i=0;i<books.length;i++)
{
var x = document.createElement('div');
x.className = 'book';
var y = document.createElement('h3');
y.appendChild(document.createTextNode(getNodeValue(books[i],'title')));
x.appendChild(y);
var z = document.createElement('p');
z.className = 'moreInfo';
z.appendChild(document.createTextNode('By ' + getNodeValue(books[i],'author') + ', ' + getNodeValue(books[i],'publisher')));
x.appendChild(z);
var a = document.createElement('img');
a.src = books[i].getElementsByTagName('cover')[0].getAttribute('src');
x.appendChild(a);
var b = document.createElement('p');
b.appendChild(document.createTextNode(getNodeValue(books[i],'blurb')));
x.appendChild(b);
document.getElementById('writeroot').appendChild(x);
}
}
function getNodeValue(obj,tag)
{
return obj.getElementsByTagName(tag)[0].firstChild.nodeValue;
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<div id="writeroot"></div>
</body>
</html>
Thanks in advance.:thumbsup:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script language="javascript">
var req=null;
var console=null;
var READY_STATE_UNINITIALIZED=0;
var READY_STATE_LOADING=1;
var READY_STATE_LOADED=2;
var READY_STATE_INTERACTIVE=3;
var READY_STATE_COMPLETE=4;
function initXMLHTTPRequest () {
var o;
try{
o = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
o = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){
o = false
}
}
if(!o && typeof XMLHttpRequest!='undefined'){
o = new XMLHttpRequest();
}
// o.async=false;
return o;
}
function sendRequest (url,params,HttpMethod) {
if (!HttpMethod) {
HttpMethod="GET";
}
req=initXMLHTTPRequest();
if (req) {
req.onreadystatechange=onReadyState;
req.open(HttpMethod,url,true);
req.setRequestHeader ("Content-Type", "text/xml");
req.send(params);
}
}
function onReadyState() {
var ready=req.readyState;
var data=null;
if (ready==READY_STATE_COMPLETE) {
data=req.responseText;
setDataXML(req);
}
else {
data="loading...["+ready+"]";
}
}
window.onload=function() {
document.getElementById('writeroot');
sendRequest("books.xml");
}
function setDataXML(req)
{
var books = req.responseXML.getElementsByTagName('book');
for (var i=0;i<books.length;i++)
{
var x = document.createElement('div');
x.className = 'book';
var y = document.createElement('h3');
y.appendChild(document.createTextNode(getNodeValue(books[i],'title')));
x.appendChild(y);
var z = document.createElement('p');
z.className = 'moreInfo';
z.appendChild(document.createTextNode('By ' + getNodeValue(books[i],'author') + ', ' + getNodeValue(books[i],'publisher')));
x.appendChild(z);
var a = document.createElement('img');
a.src = books[i].getElementsByTagName('cover')[0].getAttribute('src');
x.appendChild(a);
var b = document.createElement('p');
b.appendChild(document.createTextNode(getNodeValue(books[i],'blurb')));
x.appendChild(b);
document.getElementById('writeroot').appendChild(x);
}
}
function getNodeValue(obj,tag)
{
return obj.getElementsByTagName(tag)[0].firstChild.nodeValue;
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<div id="writeroot"></div>
</body>
</html>