Forum Moderators: open

Message Too Old, No Replies

JavaScrip that phrases XML works in FF but not IE

         

clkdesign

2:33 am on Jun 26, 2009 (gmt 0)

10+ Year Member



Okay so I'm new to JavaScript (really really green) and have been using w3schools.com to start learning the basics, but I'm having troubles getting IE 6 to read my XML document (testing on 6.0.2 to be exact) my code works in fire fox but not IE. Here is what I'm using from w3schools:

function loadXMLDoc(dname)
{
var xmlDoc;
if (window.XMLHttpRequest)
{
xmlDoc=new window.XMLHttpRequest();
xmlDoc.open("GET",dname,false);
xmlDoc.send("");
return xmlDoc.responseXML;
}
// IE 5 and IE 6
else if (ActiveXObject("Microsoft.XMLDOM"))
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load(dname);
return xmlDoc
}
alert("Error loading document");
return null;
}

And here is how its being called in the code:

function firstLinkarea()
{
var xmlDoc=loadXMLDoc("links.xml");
var txt=xmlDoc.getElementsByTagName("title")[0].getAttribute("url");
document.write('<a href="');
document.write(txt);
document.write('">');
return ("");
}

I've been looking for a fix for this but have found no, except people with the same problem I do, even a book I got for DOM has close to the same code.

Fotiman

1:35 pm on Jun 26, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



1. Your firstLinkarea function is using document.write. This means that you can ONLY call that function while the page is still loading. You can't use document.write after the page has loaded or it will replace the current document.

2. Your firstLinkarea function is only writing out an opening tag. Was that your desired behavior?

It seems to work for me in IE8 (I don't have IE6 available to test on). Here's my working example:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" >
<title>Untitled</title>
</head>
<body>
<script type="text/javascript">
function loadXMLDoc(dname) {
var xmlDoc;
if (window.XMLHttpRequest) {
xmlDoc = new window.XMLHttpRequest();
xmlDoc.open("GET", dname, false);
xmlDoc.send("");
return xmlDoc.responseXML;
}
// IE 5 and IE 6
else if (ActiveXObject("Microsoft.XMLDOM"))
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.load(dname);
return xmlDoc
}
alert("Error loading document");
return null;
}
/*
* This function is not safe. It can only be called while the page is loading
* because it uses document.write
*/
function firstLinkarea() {
// Note, I've added a dynamic parameter to the request (to prevent caching)
var xmlDoc = loadXMLDoc("books.xml?ck=" + (new Date()).getTime());
if (xmlDoc != null) {
var txt = xmlDoc.getElementsByTagName("title")[0].getAttribute("url");
document.write('<a href="');
document.write(txt);
document.write('">');
document.write(txt);
document.write('<\/a>');
}
}
firstLinkarea();
</script>
</body>
</html>

And here is the contents of links.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="web">
<title url="http://www.example.com/JavaScript">JavaScript</title>
<author>Fotiman</author>
</book>
<book category="animals">
<title url="http://www.example.com/dogs">Dogs</title>
<author>Unknown</author>
</book>
</bookstore>

Hope that helps.

[edited by: Fotiman at 1:39 pm (utc) on June 26, 2009]

clkdesign

7:36 pm on Jul 16, 2009 (gmt 0)

10+ Year Member



Hi, thanks for the reply- it has taken me a while to get back to this.

Yes this code does work in later versions of IE but not IE6; as much as I would like to drop IE6 by the wayside, the last I read about browser statistics IE6 still had %16 of the market, which is a lot depending on what type of web service you are providing.

So if document.write is over written, what would be a better thing to use? A Learning JavaScript, O'reilly book I picked up uses document.writeln in almost all of the examples but I'm not sure what the difference is or if there is one.