Forum Moderators: open
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.
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>
<?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>
[edited by: Fotiman at 1:39 pm (utc) on June 26, 2009]
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.