-2

I had getted a Uncaught SyntaxError: Unexpected identifier error when I use Java variable value. The java code is :

    <%!String questionId = "";
  String currentUserId = "";%>
<%
    String id = request.getParameter("questionId");
    if (id != null) {
        questionId = request.getParameter("questionId");
    }
    LoginResult loginResult =     (LoginResult)request.getSession().getAttribute(I_SsoLogin.LOGINRESULT);
    currentUserId = loginResult.getUser().getUserId();

%>

But the variable using in JavaScript code:

function getAnswerAdoptView(item){

    var adoptHtml = "";
    var id = "adopt_"+item.answerid;
    var userId = <%=currentUserId%>;

}

in the browser show errors:Uncaught SyntaxError: Unexpected identifier

Vivek Nerle
  • 304
  • 2
  • 14

2 Answers2

0

JSTL is not available in JavaScript resources. If you want your variable to be accessible from JavaScript you will need to declare it in a script tag in you HTML like:

<script>var userId = <%=currentUserId%></script>
kalsowerus
  • 998
  • 8
  • 23
-1

Put it inside a string like this:

var userId = "<%=currentUserId%>";
Kinshuk Lahiri
  • 1,468
  • 10
  • 23