Story background: when writing a Java Web project and running under Firefox, firebug reports an error syntax error: expected expression, got ‘;’ Or syntaxerror: expected expression, got ‘if’, etc
Click the detailed code of error reporting on the firebug console and find that the code looks like this:
$(document).ready(function(){
var s = ;
if(s==null||s==""){
alert("Please Login");
location.href="/SSM0413/jsp/login.jsp";
}
});
But the code I wrote under eclipse is as follows:
$(document).ready(function(){
var s = ${sessionScope.user.userName};
if(s==null||s==""){
alert("Please Login");
location.href="/SSM0413/jsp/login.jsp";
}
});
It is obvious that the variable assignment part ${sessionscope. User. Username} is gone. Can I use El expressions directly in JavaScript?The answer is: no
So how can we assign the value in the session to the variable in the JavaScript function?After searching the data, we get the following idea:
Write a span tag in the body and use the El expression in the span tag. However, in order not to create an additional span influence interface out of thin air, set the hidden attribute to hide it, such as:
<span id="uu" hidden="hidden">${sessionScope.user.userName}</span>
Then use getelementbyid in JS to get the content in the span tag, and you can assign a value to the variable
var s = document.getElementById("uu").innerHTML;
Summary: 1. El expressions cannot be used directly in JavaScript
2. In case of this problem, you can check the JS code first. In addition to the format problem, write more or less “;” Also, check whether some functions, libraries, or El expressions of the application can be used in JS code
3. Making good use of firebug can solve many problems