jsp - Syntax error occured when retrieveing JSTL variables from JQuery -
i want use jsp variables in jquery. please guide me how figure out..
here code in jsp..
<c:set var="fontclass" value="unicode ieunicode" />
and in js file , retrieve , out put jquery . ..
(1). var fontclass = '${fontclass}'; console.log("result values are--- "+fontclass); (2). console.log("result values are--- "+$('fontclass');
but , these 2 things can not satisfied problems. , fond article how use jstl var in jquery. , tried that...
jsp ::
<c:set var="fontclass" value="unicode ieunicode" /> <script type="text/javascript"> var fontclass = "${fontclass}"; </script>
js file::
var fontclass = '${fontclass}'; alert($("#${fontclass}"));
but got error: syntax error, unrecognized expression: #${fontclass}
what wrong codes or suggestion ? in advance..
you can't jstl <c:set/>
value in js file this. jsp processed @ server side , js @ client side.
to use value in js file :
jsp file:
<c:set var="fontclass" value="unicode ieunicode" /> <script type="text/javascript"> yourjsfunction("${fontclass}"); </script>
js file:
function yourjsfunction(fontclass) { // finalresult local variable var finalresult = fontclass; }
to use value in inline javascipt code (not good) :
jsp file:
<c:set var="fontclass" value="unicode ieunicode" /> <script type="text/javascript"> // finalresult global variable var finalresult = "${fontclass}"; </script>
/!\ not practice because global variable has common name , accessible anywhere. have @ next solution :
to use value in inline javascipt code (better way using namespace) :
jsp file:
<!-- define namespace, global object --> <script> var yourcompany = yourcompany || {}; </script> <c:set var="fontclass" value="unicode ieunicode" /> <script type="text/javascript"> // set values want namespace yourcompany.fontclass = "${fontclass}"; yourjsfunction(); </script>
js file:
function yourjsfunction() { // font class value set in namespace accessible : yourcompany.fontclass; }
Comments
Post a Comment