Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts

    parameter in function call is not defined

    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:

    Code:
    <a href="#" onClick="ebookCategory(marketing);">Marketing</a>
    Here is my js function:

    Code:
    	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:

    Code:
    <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

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Code:
    <a href="#" onClick="ebookCategory('marketing');">Marketing</a>
    Look very carefully at the difference between your code and mine.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    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">
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,246
    Thanks
    12
    Thanked 340 Times in 336 Posts
    executing ebookCategory() will kill your page! (because of document.write())
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •