Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
21
rated 0 times [  26] [ 5]  / answers: 1 / hits: 65344  / 14 Years ago, mon, november 8, 2010, 12:00:00

Is it possible to use jstl inside javascript?



I'm tying to set <c:set var=abc value=yes/>



and then later access this value in html and execute some code. My problem is the c:set is executing always even if the javascript condition is false.



<script type=text/javascript>
var v1 = false;
<c:set varabc value=yes/>

if(v1){
<c:set varabc value=no/>
}
</script>


In the above code, even if v1 is false 'no' is setting to abc.


More From » html

 Answers
6

There is no meaning to the idea of being inside Javascript when you're talking about server-side JSP interpretation. There's just no such thing as Javascript as far as that domain is concerned.



Thus, yes, it's possible, if you mean that you want to do something like



var something = '${abc}';


Note that you have to be careful with quoting and special characters. The JSTL fn:escapeXml() function is useless when you're interpolating a JSP variable into a part of the page that will be interpreted as Javascript source by the browser. You can either use a JSON library to encode your strings, or you can write your own EL function to do it.


[#95038] Friday, November 5, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
piper

Total Points: 734
Total Questions: 93
Total Answers: 112

Location: Burundi
Member since Wed, Apr 6, 2022
2 Years ago
;