jstl inside javascript
There is no meaning to the idea of being "inside Javascript" when you're talking about server-side JSP interpretation. There's just no such thing as Javascript as far as that domain is concerned.
Thus, yes, it's possible, if you mean that you want to do something like
var something = '${abc}';
Note that you have to be careful with quoting and special characters. The JSTL fn:escapeXml()
function is useless when you're interpolating a JSP variable into a part of the page that will be interpreted as Javascript source by the browser. You can either use a JSON library to encode your strings, or you can write your own EL function to do it.
Reading a jstl variable in javascript code.
Just write the Expression Language directly in your JavaScript code:
$("#textBoxInp").keyup(function() {
var userId = '${userId}';
});
Note that this won't work if the JavaScript code is placed in a external file and is invoked in the JSP. In this case, you may refer to one of the four ways that BalusC explain here: Mixing JSF EL in a Javascript file (he explains five, but one of them is JSF specific).
How to access JSTL tag from JavaScript
This is JSP
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<script>
function displayErrors() {
alert("<form:errors path='*'>");
}
</script>
You can use JSTL tags inside JSP file, not JS file.
JavaScript inside jstl iteration
It's not the right way to do it, but a simple solution would be use addEventListener
instead onload
:
<%int number=0;%>
<c:forEach var="row" items="${tAdmin.rows}" varStatus="totalRow" step="1">
<td><%=++number%></td>
<td>
<div id="content<%=number%>" style="table-layout:fixed; width:405px; word-wrap:break-word;">
<script language="JavaScript" type="text/JavaScript">
window.addEventListener("load", function () {
var element = document.getElementById("content<%=number%>");
element.innerHTML=Utf8.decode('${row.content}');
}, true);
</script>
</div>
</td>
</c:forEach>
In fact your code is using only the last "onload" because, when loading the page, it will execute the javascript load
callback only when finish full loading it. So, each time you loop is executed, it updates the load
callback reference for the last one, so when onload
is triggered, the last only will be executed.
But your code has other errors too. The content
id, repeats at the code lot of times, that will make your div getElementById
useless, because you have lot of ids that are equal. Ids must be unique to work property.
To finish, it's not a good pattern to mix your HTML with scripts inside, is better to have you logic file (javascript file) outside, then it can make changes in your code when finish to load, reading the html that was generated. You also can create data
attribute in your div
then read it by the javascript to manage all itens with a specific data
attributes.
To keep it simple, I will add an example:
<%int number=0;%>
<c:forEach var="row" items="${tAdmin.rows}" varStatus="totalRow" step="1">
<td><%=++number%></td>
<td>
<div id="content<%=number%>" style="table-layout:fixed; width:405px; word-wrap:break-word;" data-content="${row.content}">
</div>
</td>
</c:forEach>
Now the script file (I'm using jQuery for this example works on any browser):
$(function() {
$("[data-content]").each(function(item) {
$(item).html(Utf8.decode(item.attr('data-content')));
});
});
Javascript file not accessing jstl
JSPs are interpreted by server before producing output to the browser, therefore your tags are being interpreted before being served to the browser. With standard configuration JS files are are not interpreted by the server, just passed through to the client as plain text.
If you want to produce JS dynamically with JSP (as in your example), you need to make server to interpret file before serving it to the client. The easiest way it would be to put content of your JS into JSP file. Then on the html page include script with the script tag and attribute src equals your.jsp, e.g.
<script src="script.jsp"></script>
Related Topics
Get Image from Json File Using JavaScript and Display in HTML Img Tag
How to Add Query String Parameter to Same Url When on Click a Href Link
How to Change Font-Size in Vuetify Component
What Is the Purpose of the Dollar Sign in JavaScript
How to Prevent Invalid Characters from Being Typed into Input Fields
React Typescript Is Not Assignable to Parameter of Type
How to Wait Until 2 $Http Requests End in Angularjs
If a Div Contains a Specific String of Text, Edit the Parent'S CSS
How to Filter Date Range in Datatables
Setting Cross-Domain Cookies in Safari
Typescript Module Has No Exported Members - React
Textarea With Initial Auto Height by Content With Pure CSS
Datatables - Search in Multiple Columns With a Single Drop Down
Increase JavaScript Heap Size in Create-React-App Project
Check Whether a String Contains Only Special Characters
Simple Pagination in JavaScript