Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSTL and Javascript

Is having tags inside a jstl tag considered bad form? So for example I know the following will work but is it considered bad form to have the script tags inside my jstl tags in my jsp ?

<c:choose>
  <c:when test="${!empty bean.value}">
    <p>Its not empty</p>
  </c:when>
  <c:otherwise>
    **<script>
        callJSSomeFunction();
    </script>**
  </c:otherwise>
</c:choose>
like image 606
b3labs Avatar asked Aug 02 '26 00:08

b3labs


1 Answers

I don't know about bad form (a bit harsh) but you might consider that to someone viewing your page without JS enabled your <c:otherwise> will effectively output nothing, and that's not very graceful.

Also, if the page is rendered incrementally your function call might execute as it has been ouput, and before the DOM has been fully loaded by the browser and thus behave unpredictably (or just not work at all - I've had this happen).

I would consider putting all function invocations in the head, and use one of the many available tricks for detecting when the DOM has loaded (jQuery's $(document).ready() for example) to enforce a neater separation, and make your life that much easier.

like image 184
karim79 Avatar answered Aug 03 '26 13:08

karim79



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!