–Updated Code to make it…more simple–
Kind of a strange situation, according to w3schools my code should work, but for some reason it doesn’t…connect. Here’s my code for my java script.
XHTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
</head>
<body>
<p id="intro">Hello</p>
<script type="text/javascript" id="announce" src="announce.js"></script>
</body>
</html>
JavaScript
/*jscript*/
var newannouncement = document.createElement('p');
newannouncement.id = 'announcing';
newannouncement.appendChild(document.createTextNode('Here is an announcement'));
var scr = document.getElementById('announce');
scr.parentNode.insertBefore(newannouncement, scr);
if(window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
} else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
**xmlhttp.open("GET", "catalog.xml",false);**
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var x=xmlDoc.getElementsByTagName("cd");
for (i=0; i<x.length; i++)
{
alert("for")
}
XML
<?xml version="1.0"?>
<catalog
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="catalog.xsd">
<cd>
<author>Ferdi</author>
<date>1192</date>
</cd>
<cd>
<author>JRR</author>
<date>1995</date>
</cd>
</catalog>
Update 1
Due to the fact that I considered the previous code, too confusing, I’ve changed it to make it simpler so that I can actually find out what the heck is cracking off. The problem appears to be the xmlhttp.open() as the for loop doesn’t run. I’ve switched to xhtml as it keeps the code…easier to read (for me) and I understand that document.write() doesn’t work.
The problem
I simply just call the function in-line. The *‘s indicate the line that fails. Now, I’ve tried just leaving it as announce.xml (leaving the announce.xml in same directory) and I’ve now tried moving it down a directory. I’ve also tried adding /../ but to no avail.
I didn’t see any ‘technical’ requirements on the w3schools website…so I’m assuming like html it works out of the box.
Further notes
If possible if you’re going to suggest technology, such as AJAX, could you please describe what it is, what it does and if possible a link to a suitable place to find out more. Thanks
(This isn’t really an answer, but a comment, however I have several points I’d like to make which is easier in an answer).
Are you using a debugger with your browser (FireBug extention for Firefox, or the one built into Chrome or IE)? Is the Console in the debugger reporting an error? Also debuggers let you track the requests the browser makes. Does it show your request?
There are several points independent from your problem:
document.write. It only works during page loading and because you are doing a synchronous request. In other cases you can easily “overwrite” your page with it.EDIT:
The problem is most likely what @bazmegakapa suggested:
XMLHttpRequestusually only works when on a web server. What you need to do is install a web server locally on your development computer and use it for testing. This is usually quite simple. Even Apache is quickly installed and runs with little configuration. Or a package with Apache and including a DB server (MySQL) and PHP called “AMP” which are often specifically made for local web page development and testing and are setup even easier. And finally many web development IDEs have a simple web server built in (I like Aptana Studio, for example).What you are doing is commonly called “AJAX” which stands for “Asynchronous JavaScript and XML”, except you are not doing it asynchronously, which you should not. Have a look at a short tutorial from the Mozilla Developer Network work how to do it right: https://developer.mozilla.org/en/AJAX/Getting_Started
If you still have problems, then you’ll need to give more detailed information, most importantly the error message the browser gives in its console (see my remark on debuggers above).