I have my jstl code inside a javascript file that i am including inside my jsp page. The problem I have is that when i write my jstl code inside a script inside the jsp page it is working fine. but when i write the same code in a separate js file, the jstl code does not work at all. Is there anyway I can get around this? Any help would be appreciated. Here is my code below. Thanks
$("img[name='cancel']").hover(function(){
var src = "<c:url value='/images/stop.ico'/>";
$(this).attr("src",src);
},function(){
var src = "<c:url value='/images/gtk_close.ico'/>";
$(this).attr("src",src);
});
I have my jstl code inside a javascript file that i am including inside my jsp page. The problem I have is that when i write my jstl code inside a script inside the jsp page it is working fine. but when i write the same code in a separate js file, the jstl code does not work at all. Is there anyway I can get around this? Any help would be appreciated. Here is my code below. Thanks
$("img[name='cancel']").hover(function(){
var src = "<c:url value='/images/stop.ico'/>";
$(this).attr("src",src);
},function(){
var src = "<c:url value='/images/gtk_close.ico'/>";
$(this).attr("src",src);
});
Share
Improve this question
asked Jun 3, 2013 at 19:50
Katakam NikhilKatakam Nikhil
1,3854 gold badges14 silver badges22 bronze badges
6
- JSP gets interpreted server side. I don't see how this could work. – crush Commented Jun 3, 2013 at 19:51
-
Assuming you are including your JS file with a
<script>
element this include is done by the browser. – nnnnnn Commented Jun 3, 2013 at 19:52 - it is working correctly if i place my script tag inside the jsp. If i do the same by including the external js file with a script tag, it doesnt work. – Katakam Nikhil Commented Jun 3, 2013 at 21:04
- The problem is that your JS files must contain, uhm, JavaScript code only. JSTL works only on view (in this case, your JSP files) not outside of it. The best for you will be having variables in your JS files and fill these in your JSP using the respective EL/JSTL code. – Luiggi Mendoza Commented Jun 3, 2013 at 21:17
- Thanks Luigi, thats what I needed to know. – Katakam Nikhil Commented Jun 6, 2013 at 2:14
1 Answer
Reset to default 9JSPs 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>