wood burning stoves 2.0*
The moose likes JSP and the fly likes Passing the JSTL variable (with special characters) to JavaScript function? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Passing the JSTL variable (with special characters) to JavaScript function?" Watch "Passing the JSTL variable (with special characters) to JavaScript function?" New topic
Author

Passing the JSTL variable (with special characters) to JavaScript function?

Muralidharan Raju
Greenhorn

Joined: Feb 24, 2011
Posts: 25

Hey folks.. you people are doing just amazing job replying to every post..

i have a question where i am stuck totally.

how can i pass a JSTL variable which contains special characters..? (this is been bugging me since yesterday. F1 guys).
need to escape these characters.

Suppose :-


<c:set var="text1" value="special!%&'<>characters" />

<input type="button" name="button1" onclick="special('${text1}')" >

--------------------------------------------------------------------------------------

i am not able to pass the the JSTL variable itself.
in mozilla error console shows : "unterminated string litteral" ..

Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 448

Welcome to JR

In the string
special!%&'<>characters
the presence of ' is treated as start of new String.

What happens when you used the /' instead of '?


Java Experience
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

You need to escape quote characters when they are places within quoted strings.

You can use <c:out> for this.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Muralidharan Raju
Greenhorn

Joined: Feb 24, 2011
Posts: 25

how do i assign it to another JSTL variable..? before sending it to JavaScript..

<cut value="special!%&'<>characters" />

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

You're not actually "sending" the variable to JavaScript, so no. Remember that JSP runs on the server in order to create an HTML page to send to the browser. Once the page leaves the server, there's no more JSP-ness about it, so there's no concept of "sending" a variable to javaScript. You are merely dynamically creating an HTML page.

So:

Or, for places where a tag cannot be used (or where it's more readable), output can also be HTML-escaped by using the fn:escapeXml() JSTL function as such:
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
Bear Bibeault wrote:You need to escape quote characters when they are places within quoted strings.

You can use <c:out> for this.


The <c:out> will escape characters for printing into html/xml.
So & will become &amp;, < becaomes &lt; etc etc

That won't help in this particular case, where what you want to escape is in the context of a javascript string.

I would recommend leveraging the commons StringEscapeUtils for this job.

Muralidharan Raju
Greenhorn

Joined: Feb 24, 2011
Posts: 25

Bear Bibeault wrote:You're not actually "sending" the variable to JavaScript, so no. Remember that JSP runs on the server in order to create an HTML page to send to the browser. Once the page leaves the server, there's no more JSP-ness about it, so there's no concept of "sending" a variable to javaScript. You are merely dynamically creating an HTML page.

So:

Or, for places where a tag cannot be used (or where it's more readable), output can also be HTML-escaped by using the fn:escapeXml() JSTL function as such:


Yes Sir, the cut and escapeXml worked but now unfortunately, i am getting an "Unterminated String litteral" error. prefixing with single quatoation(') also still didnt work..? any other idea?? way?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

Take a look at the HTML/JavaScript being sent to the browser so you can spot the syntax error and figure out where it's coming from.
Muralidharan Raju
Greenhorn

Joined: Feb 24, 2011
Posts: 25

Sir,
i can see it in the mozilla error console. showing :

Unterminated String litteral
special('value >= 120
...........^.......................................


Thing is its not able to send to the JS itself. when i call the onClick method.
i am getting this in javaScript as

function special(specialvalue)
{
....
...
}
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
As I said earlier...

Stefan Evans wrote:
The <c:out> will escape characters for printing into html/xml.
So & will become &amp;, < becaomes &lt; etc etc

That won't help in this particular case, where what you want to escape is in the context of a javascript string.

I would recommend leveraging the commons StringEscapeUtils for this job.



The problem you are hitting is that the quote symbol isn't being escaped. You need ' to be turned into \' when in the context of a javascript string.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

This is why you need to look at what's being sent to the browser. It's not always clear from the looking at the source what the context of the problem will be and which encoding/escaping is necessary.
Muralidharan Raju
Greenhorn

Joined: Feb 24, 2011
Posts: 25

Stefan Evans wrote:As I said earlier...


The problem you are hitting is that the quote symbol isn't being escaped. You need ' to be turned into \' when in the context of a javascript string.


Sir, the problem is..
(where adv is a collection.)

i am using the onclick method in the tr tag.



Error: missing ) after argument list
Source File: http://localhost:8080/....
Line: 1, Column: 26
Source Code:
click(8,'low',TEMP (C) <= 120
------------------------------^--------



Error: unterminated string literal
Source File: http://localhost:8080/....
Line: 1, Column: 14
Source Code:
click(8,'low','TEMP (C) <= 120
--------------^---------------

if i use \' as you said..



Error: illegal character
Source File: http://localhost:8080/...
Source Code:
click(8,'low',\'TEMP (C) <= 120
--------------^----------------

i copied the error from error console of mozilla
i even tried various syntax ways .. :( this error is bugging me since past five days..
Muralidharan Raju
Greenhorn

Joined: Feb 24, 2011
Posts: 25

Bear Bibeault wrote:This is why you need to look at what's being sent to the browser. It's not always clear from the looking at the source what the context of the problem will be and which encoding/escaping is necessary.


the value that i get in the browser is (which is sent from the table ) is a string with special characters. i have showed in the previous post.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016

I presume that what you want to generate is something like this:



When I mentioned the backslash(\), I mean that the backslash needs to be used within a javascript string so that you can include a quote. otherwise the quote would terminate the string.
So if you wanted the string 'Bob's Place', it would have to become 'Bob\'s Place'
"15" monitor" --> "15\" monitor"

This looks close to what you want, but it won't do the job I don't think.
onClick="click(${adv.advFilterId},'${adv.advFilterName}','${fn:escapeXml(adv.criteria)}')"


The fn:escapeXml won't work in the case containing a single quote.

Here is something for you to try out. It invokes the commons StringEscapeUtils library through an EL function
StringEscapeFunctions:



StringEscapeFunctions.tld:



and JSP that invokes it: escapeJavascriptString.jsp



None of these solutions address all of the issues, but they should give you an idea of what needs to be done.
Muralidharan Raju
Greenhorn

Joined: Feb 24, 2011
Posts: 25

Hey folks..

i finally resolved this issue using spring message tag, giving javaScriptEscape="true"

after 3 weeks of research !!

<spring:message code="${escapedString}" javaScriptEscape="true"/>
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Sorry for bumping the thread up ,but thanks a lot Stefan ! Your post regarding the custom EL function helped a lot!


Learning and Learning!-- Java all the way!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing the JSTL variable (with special characters) to JavaScript function?
 
Similar Threads
Javascript escape characters in a JSP
XMLHttpRequest issue while using Special characters
Issue with special characters in javascript
Getting attachment filename with special characters(JAVA MAIL API)
Struts2 variable inside javascript