Quick Javascript problem. Any ideas?

Soldato
Joined
12 Sep 2003
Posts
10,850
Location
Newcastle, UK
Thanks in advance for any pointers, basically, I'm trying a simple Javascript program to retrieve data from a .XML file with DOM. The W3C Schools website gives me this example,

Code:
<html>
<head>
<script type="text/javascript" src="loadxmldoc.js"> 
</script>
</head>
<body>

<script type="text/javascript">
xmlDoc=loadXMLDoc("books.xml");

var x=xmlDoc.getElementsByTagName('price');
for (i=0;i<x.length;i++)
  {
  document.write(x[i].childNodes[0].nodeValue)
  document.write("<br />")
  }
</script>
</body>
</html>

So that should return the price, or if I change that value to say 'author' it returns the author. Simple!

So I saved the books.xml to my hard drive and copied that code into notepad and saved it as test.html. But when I run it locally, nothing happens?! :( I have Apache running as well.

Any ideas please?

Thanks!!!

EDIT: Here's the link where you can run the example,

http://www.w3schools.com/dom/dom_nodes_get.asp
http://www.w3schools.com/dom/tryit.asp?filename=try_dom_getelementsbytagname
 
Last edited:
Bloo_Fish said:
Are you sure the path to your XML file is correct?



If the files are both in the same location/directory then the example should be fine - if the files arnt together then you will need to specify the path to your XML file.


Also do you have the file: <script type="text/javascript" src="loadxmldoc.js">?

Thanks for the reply, both the .xml file and the .html /.php file are in the same folder, such I try hard-coding a path in. I don't have a "loadxmldoc.js" file, but I thought that might just be something that is called in the background kind of thing.

? :confused:
 
Ahhh cheers for that, it seems to work now! :cool: yey!

I'm sure if I get stuck I'll ask again. :) Thanks again!!
 
Back
Top Bottom