Is there a way to pass JSP JSTL array to a Javascript function?

I am able to pass a single JSTL variable to a javascript function, however I need to pass a String array. In my page users can click the word "noun" and it will highlight all the nouns in the page. So in the text on the page the nouns already have an id associated with them (the this information is dynamically generated from another Java program I have).

Right now I tried.

<span onclick="highlightTerm('${arrayOfIds}')">Noun</span>

That does not work (javascript error because I try to iterate through a list and get elements which do not exist)

Do I have to build a javascript array separately and add values one by one?

[695 byte] By [smiles78a] at [2007-11-27 1:31:04]
# 1
Remember that the JSTL runs on the server and that the Javascript runs on the client, after the JSTL has finished. In the JSTL you are generating the Javascript.
DrClapa at 2007-7-12 0:33:13 > top of Java-index,Java Essentials,Java Programming...
# 2
I didn't understand what you meant. I'm able to call a javascript function with a JSTL variable if it's a single string, but not an array.
smiles78a at 2007-7-12 0:33:13 > top of Java-index,Java Essentials,Java Programming...
# 3

> I didn't understand what you meant.

I think it's pretty clear and simple.

Step 1. On the server, the JSP (including JSTL) runs. It outputs HTML. It also outputs Javascript. Note that no Javascript is run at this time.

Step 2. The HTML and Javascript travels across the network.

Step 3. The browser renders the HTML and executes the Javascript as required. Note that there are no JSTL variables available here. The Javascript that runs is whatever you generated in Step 1. Also note that you can use the "View Source" option in the browser to look at the HTML and Javascript that you generated.

Thinking that you can pass a JSTL variable to a Javascript function is counterproductive because you can't. It's better to think of things the way they really are and not in lazy shortcut ways.

DrClapa at 2007-7-12 0:33:13 > top of Java-index,Java Essentials,Java Programming...
# 4
If you want to pass an array from Java to Javascript, you can't simple assign it.You have to iterate the whole array in Java and assign those values one by one to Javascript.By the way, if you want to communicate with the server dynamically with Javascript, check AJAX.
rym82a at 2007-7-12 0:33:13 > top of Java-index,Java Essentials,Java Programming...
# 5

> Thinking that you can pass a JSTL variable to a

> Javascript function is counterproductive because you

> can't. It's better to think of things the way they

> really are and not in lazy shortcut ways.

I find that pretty rude of you to say since I do have code:

<span onclick="toggleContents('${id}')">Some value </span>

This works just fine. I was just asking if I can pass an array.Maybe you should get your facts straight before telling someone you can't even pass the variable at all.

smiles78a at 2007-7-12 0:33:13 > top of Java-index,Java Essentials,Java Programming...
# 6

> This works just fine. I was just asking if I can

> pass an array.Maybe you should get your facts

> straight before telling someone you can't even pass

> the variable at all.

My facts are straight. You aren't passing a variable at all there. You are generating some HTML with Javascript that looks like this:<span onclick="toggleContents('something')">Some value </span>

If you use the "View Source" option in your browser you can see that for yourself. Sure it works, in the sense that it does what you want. It just doesn't work the way you are thinking of it.

And yeah, "lazy" wasn't the right word. I apologize for that.

DrClapa at 2007-7-12 0:33:13 > top of Java-index,Java Essentials,Java Programming...