Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Muralidharan Raju
Greenhorn
Posts: 25
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 '?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to escape quote characters when they are places within quoted strings.

You can use <c:out> for this.
 
Muralidharan Raju
Greenhorn
Posts: 25
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do i assign it to another JSTL variable..? before sending it to JavaScript..

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

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1784
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1784
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1784
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 25
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 477
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for bumping the thread up ,but thanks a lot Stefan ! Your post regarding the custom EL function helped a lot!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic