passing variable problem or quotation syntax conundrum

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • carllucas
    New Member
    • Oct 2006
    • 35

    #1

    passing variable problem or quotation syntax conundrum

    I'm having issues trying to pass a variable

    basically I get information from an external xml document like this

    Code:
    function run(file) {
    	t = file.getElementsByTagName("category");
    
    	for(i=0; i<t.length; i++) {
    	  element = file.getElementsByTagName("category")[i].childNodes[0].nodeValue;
    	  theMenu +=	"<a href='javascript:open(f, "+
    			element+")'>"+
    			element+
    			"</a><br>";
    	}
    
    
    }
    What this does is create a list of the elements tagged "category" with a respective link to a javascript function. this part works because when i hover over the list i get something like "javascript : open(Something) " in the status bar.

    to test this i create this function:
    Code:
    function open(msg) {
       alert(msg);
    }
    However when i click on the links i get an error:

    Code:
    Error: 'Something' is undefined
    I think what it is trying to do is find out what is assaigned to 'Something' (or how 'Something is defined)but 'Something' is the definition. 'Something is the definition of element at that part of the for loop.

    I figured that a way to solve this is to tell the machine that whatever is assigned to element should be considered a string. But I don't know how to deal with the syntax of 3 quotes in each other because I would essentially need something like this:

    Code:
    	  theMenu +=	"<a href="javascript:open(f, ""+
    			element+"")">"+
    			element+
    			"</a><br>";
    Notice the 3 "s which ones will be "s and which ones will be 's and what about the 3rd one? Is it a " or a '. I've tried all sorts of combos but I can't figure out the right syntax.

    Is there another way around this error? Sorry for the length of this... any help appriciated.
  • iam_clint
    Recognized Expert Top Contributor
    • Jul 2006
    • 1207

    #2
    Code:
    theMenu +="<a href=javascript:open(f, '"+element+"')>"+element+"</a><br>";

    Comment

    • carllucas
      New Member
      • Oct 2006
      • 35

      #3
      Originally posted by iam_clint
      Code:
      theMenu +="<a href=javascript:open(f, '"+element+"')>"+element+"</a><br>";
      I've tried this and it doesn't work.

      when I hover over the link the status bar shows "javascript : open(f, "

      and when you click the link you get:

      "Error: syntax error"

      How for example do internet email apps allow the user to click a message title and follow the link. Is there a way I can use this methodoly with XML instead of PHP?

      Comment

      • carllucas
        New Member
        • Oct 2006
        • 35

        #4
        This is my XML document named myLibrary.xml

        Code:
        <?xml version="1.0" encoding="ISO-8859-1"?>
        <library>
        
        <book>
          <title>My Family</title>
          <category>Biography</category>
          <author>John Smith
          <year>2004</year>
          <price>30.00</price>
        </book>
        
        <book>
          <title>Spooks</title>
          <category>Thriller</category>
          <author>Eric McGee
          <year>2000</year>
          <price>10.99</price>
        </book>
        
        <book>
          <title>New York</title>
          <category>Travel</category>
          <author>Ellis June
          <year>1998</year>
          <price>13.99</price>
        </book>
        
        <book>
          <title>Lonely Planet Tokyo</title>
          <category>Travel</category>
          <author>Andrew Bender</author>
          <year>2006</year>
          <price>19.99</price>
        </book>
        
        <book>
          <title>Angela's Ashes: A Memoir</title>
          <category>Biography</category>
          <author> Frank McCourt</authro>
          <year>1999</year>
          <price>14.95</price>
        </book>
        
        </library>
        I could like to create a list of all the categories and when you click on the categories it takes you to the books in those categories.

        I'm sure someone's had to do this before can someone please help me.

        Comment

        Working...