I have a JavaScript function which does not give the result I want.
Here is the code (it is a part of a JavaScript function):
<%
String myStringVariable = "no";
%>
alert("yes"); // This alert gets shown.
alert(<%=myStringVariable%>); // But this alert doesn't get shown.
I do not understand why the second alert does not work. Can someone identify the reason?
Like as in Java, in JavaScript should strings be quoted.
You need to realize that this code doesn’t run in sync. It’s Java/JSP which runs first, generates a bunch of HTML/CSS/JS and sends it to the browser. Then it’s HTML/CSS/JS turn to run. If you do a rightclick, View Source in browser, you’ll see that Java/JSP has already done its job and there should be no single line of it.
See also: