aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Passing JSTL or EL as parameter in javaScript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Passing JSTL or EL as parameter in javaScript" Watch "Passing JSTL or EL as parameter in javaScript" New topic
Author

Passing JSTL or EL as parameter in javaScript

Darren Alexandria
Ranch Hand

Joined: Aug 17, 2007
Posts: 185
Good day!

Is it possible to pass JSTL or EL as parameter in javaScript?

Here's a snippet:


but this returned an undefined value.

Thanks!
Christophe Porteneuve
Author
Ranch Hand

Joined: Jan 28, 2008
Posts: 43
Hey Darren,




but this returned an undefined value.


This looks perfectly valid, since your JS call here is apparently in the JSP. I don't think your problem lies in the script (although I assume you have a onclick handler, not a onKlick one�), but that you do not have a strMap variable handy in this context (or that this variable's toString method doesn't match your purpose).

You should double-check the presence and nature of this variable in your active scopes.


Christophe Porteneuve aka TDD
"They did not know it was impossible, so they did it." --Mark Twain
Pragmatic Guide to JavaScript: http://books.pragprog.com/titles/pg_js/
Prototype and script.aculo.us: http://books.pragprog.com/titles/cppsu/
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

Remember that this is not "passing" any data in the normal sense. The data will be inserted into the HTML/JavaScript page before being sent to the client.

As such, you can do a View Source on the page to see if the substitution happened as you expected.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Darren Alexandria
Ranch Hand

Joined: Aug 17, 2007
Posts: 185
Hello!

Thanks for your replies!
Have already solved it!

It should be like this:



God bless you all!

[ February 12, 2008: Message edited by: Darren Alexandria ]
[ February 12, 2008: Message edited by: Darren Alexandria ]
Alaa Nassef
Ranch Hand

Joined: Jan 28, 2008
Posts: 467
So the only difference was putting single quotes instead of double quotes? Weird! I thought that didn't make a lot of difference with javascript (even though I like to stick to single quotes with javascript for no apparent reason). I still don't get the onKlick handler you are using. Is it a custom handler or what?


Visit my blog: http://jnassef.blogspot.com/
Christophe Porteneuve
Author
Ranch Hand

Joined: Jan 28, 2008
Posts: 43
So the only difference was putting single quotes instead of double quotes? Weird! I thought that didn't make a lot of difference with javascript


It's not a JS issue, it's a HTML issue. The inline JS is an attribute value, and the attribute is delimited by double quotes, so putting double quotes in there breaks the attribute value (I should have seen it!).

The event handler's inline JS goes something like "insertText(", which obviously breaks.

Yet another reason why you should do away with inline JS and go unobtrusive�
Alaa Nassef
Ranch Hand

Joined: Jan 28, 2008
Posts: 467
It's really funny how sometimes you miss something that's really basic and elementary :roll: . Sorry for the double post earlier, but when I get those 503 and network error pages, I resubmit my post. I'll try to delete one of the two posts.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

Originally posted by Christophe Porteneuve:
Yet another reason why you should do away with inline JS and go unobtrusive�

Indeed. Unobtrusive JavaScript is a topic we touched upon a few weeks back, and it bears re-emphasizing.

In the same way that CSS allowed us to segregate stylistic information from the document markup, unobtrusive JavaScript separates the behavior from the markup. This gives us all the "separation of concern" benefits, and avoiding aggravating syntax boofs like our OP stumbled upon.

So rather than declaring the onclick handler inline with the elements, it should be declared in the window onload handler or the document-ready handler.

In raw JavaScript:

Using the star of the week, Prototype:

And in jQuery:

Using either of Prototype or jQuery is highly recommended as both do a pretty good job of insulating you from the bumps of browser differences.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

Originally posted by Alaa Nassef:
I still don't get the onKlick handler you are using. Is it a custom handler or what?

The forum blocks certain words from being posted as a security concern. onclick and eval are two examples.

Posters frequently misspell these purposefully to get around the filter.

Of course, with a little HTML magic, you can easily post these: onclick onclick onclick onclick onclick onclick!

By substituting the corresponding HTML entity for any one of the characters, the filtering is avoided. For example, to enter onclick, you would type:
onclick
Alaa Nassef
Ranch Hand

Joined: Jan 28, 2008
Posts: 467
Well, thanks Bear. Didn't know that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing JSTL or EL as parameter in javaScript