2

How to do the console.log for EL expression in JS file

jQuery(document).ready(function() {

    $('.js-bbCheckbox').click(function(event) {
        var bbck = '${tagOne}';
        console.log (bbck);
        console.log (${tagTwo});
});
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Shoaib Ud-Din
  • 4,614
  • 3
  • 23
  • 23
  • Please get your terms right. The `${foo}` is not a "JSTL tag". It's an "EL expression". I edited the question accordingly. In the meanwhile, please carefully read http://stackoverflow.com/tags/jstl/info to learn what JSTL really is. – BalusC Oct 23 '12 at 16:05

2 Answers2

5

To start, you need to understand that JSP (and JSTL and EL) basically produces HTML (and CSS and JS) code. It doesn't run in sync with JavaScript code. If you rightclick the JSP page in webbrowser and do View Source then you'll see it.

I think that your concrete problem is caused because the ${tagTwo} returns a plain vanilla string which is in turn by JS been interpreted as a variable name, because it isn't been enclosed in quotes.

You need to let JSP print a fullworthy JS string instead of a variable name.

console.log('${tagTwo}');
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • By doing this I am just getting it print, I would like to get the value of that tag. – Shoaib Ud-Din Oct 23 '12 at 16:07
  • Apparently that part of JS code is not been enclosed in a `.jsp` file, but in a plain `.js` file which is imported by ``. Is this true? The EL expressions will obviously only work inside a JSP file as that's been parsed by `JspServlet`. E.g. ``. – BalusC Oct 23 '12 at 16:08
  • Thats what I was trying to find out that is it possible. – Shoaib Ud-Din Oct 23 '12 at 16:09
  • See http://stackoverflow.com/questions/2547814/mixing-jsf-el-in-a-javascript-file/2547908#2547908 – BalusC Oct 23 '12 at 16:11
0

@Shoaib Ud-Din, if you don't mind a little inline code, this should work with any included .js file that is loaded after this in the DOM:

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>

<c:set var="tagOne" value="Hello World" scope="session" />
<script>
    var bbck = "<c:out value="${tagOne}" />";
    console.info("<c:out value="${tagOne}" />");
</script>
Tommaso Boggia
  • 140
  • 2
  • 12