By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,566 Members | 1,779 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,566 IT Pros & Developers. It's quick & easy.

passing variable problem or quotation syntax conundrum

P: 35
I'm having issues trying to pass a variable

basically I get information from an external xml document like this

Expand|Select|Wrap|Line Numbers
  1. function run(file) {
  2.     t = file.getElementsByTagName("category");
  3.  
  4.     for(i=0; i<t.length; i++) {
  5.       element = file.getElementsByTagName("category")[i].childNodes[0].nodeValue;
  6.       theMenu +=    "<a href='javascript:open(f, "+
  7.             element+")'>"+
  8.             element+
  9.             "</a><br>";
  10.     }
  11.  
  12.  
  13. }
  14.  
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:
Expand|Select|Wrap|Line Numbers
  1. function open(msg) {
  2.    alert(msg);
  3. }
  4.  
However when i click on the links i get an error:

Expand|Select|Wrap|Line Numbers
  1. Error: 'Something' is undefined
  2.  
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:

Expand|Select|Wrap|Line Numbers
  1.       theMenu +=    "<a href="javascript:open(f, ""+
  2.             element+"")">"+
  3.             element+
  4.             "</a><br>";
  5.  
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.
Nov 22 '06 #1
Share this Question
Share on Google+
3 Replies


iam_clint
Expert 100+
P: 1,208
Expand|Select|Wrap|Line Numbers
  1. theMenu +="<a href=javascript:open(f, '"+element+"')>"+element+"</a><br>";
  2.  
Nov 22 '06 #2

P: 35
Expand|Select|Wrap|Line Numbers
  1. theMenu +="<a href=javascript:open(f, '"+element+"')>"+element+"</a><br>";
  2.  
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?
Nov 22 '06 #3

P: 35
This is my XML document named myLibrary.xml

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <library>
  3.  
  4. <book>
  5.   <title>My Family</title>
  6.   <category>Biography</category>
  7.   <author>John Smith
  8.   <year>2004</year>
  9.   <price>30.00</price>
  10. </book>
  11.  
  12. <book>
  13.   <title>Spooks</title>
  14.   <category>Thriller</category>
  15.   <author>Eric McGee
  16.   <year>2000</year>
  17.   <price>10.99</price>
  18. </book>
  19.  
  20. <book>
  21.   <title>New York</title>
  22.   <category>Travel</category>
  23.   <author>Ellis June
  24.   <year>1998</year>
  25.   <price>13.99</price>
  26. </book>
  27.  
  28. <book>
  29.   <title>Lonely Planet Tokyo</title>
  30.   <category>Travel</category>
  31.   <author>Andrew Bender</author>
  32.   <year>2006</year>
  33.   <price>19.99</price>
  34. </book>
  35.  
  36. <book>
  37.   <title>Angela's Ashes: A Memoir</title>
  38.   <category>Biography</category>
  39.   <author> Frank McCourt</authro>
  40.   <year>1999</year>
  41.   <price>14.95</price>
  42. </book>
  43.  
  44. </library>
  45.  
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.
Nov 23 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.