...

View Full Version : parameter in function call is not defined



surreal5335
11-22-2010, 12:25 AM
I have a good understanding of javascript although I am new to working xml. So I am having a problem getting javascript to recognize that my parameter value in my function call is an attribute in my xml.

I called the js function like this:



<a href="#" onClick="ebookCategory(marketing);">Marketing</a>


Here is my js function:



var category;

function ebookCategory(category) {


if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","ebook.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;

document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName(category);
for (i=0;i<x.length;i++)
{
document.write("<tr><td>");
document.write(x[i].getElementsByTagName("bookCover")[0].childNodes[0].nodeValue);
document.write(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue);
document.write(x[i].getElementsByTagName("description")[0].childNodes[0].nodeValue);
document.write(x[i].getElementsByTagName("price")[0].childNodes[0].nodeValue);
document.write("</td><td>");
i++;
document.write(x[i].getElementsByTagName("bookCover")[0].childNodes[0].nodeValue);
document.write(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue);
document.write(x[i].getElementsByTagName("description")[0].childNodes[0].nodeValue);
document.write(x[i].getElementsByTagName("price")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
}
document.write("</table>");

} // end ebookCategory()


Some of the aspects in this function are new to me, xml stuff. Here is my xml setup:



<ebook category="marketing">
<bookCover><img src="/millionaire/ebooks/covers/miracle2.jpg" height="150" width="150" /></bookCover>
<title>Marketing Miracle</title>
</ebook>


I appreciate any help in the matter

Old Pedant
11-22-2010, 12:55 AM
<a href="#" onClick="ebookCategory('marketing');">Marketing</a>


Look very carefully at the difference between your code and mine.

Old Pedant
11-22-2010, 12:57 AM
But then your JS code is wrong.

getElementsByTagName('marketing') would tell it to look for <marketing> tags.

It would *NOT* tell it to look for <ebook category="marketing">

Dormilich
11-22-2010, 08:25 AM
executing ebookCategory() will kill your page! (because of document.write())



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum