1

I have a javascript file "util.js" which I am using in a jsp file. How to access the application context EL ${pageContext.request.contextPath} in the javascript file?

This EL is working if the javascript function is copied inside the JSP. But if I keep it in separate javascript file, this is not working.

0

2 Answers 2

3

Well, you can either have your .js file as a .jsp, but with Content-Type text/javascript (undesirable), or you can define javascript variables in your jsp that includes the .js file, and pass (or less desirable - use directly) those variables to the functions. For example:

<script type="text/javascript" src="js/scripts.js"></script>
<input type="button" 
     onclick="someExternalJavascriptFunction('${pageContext.request.foo}')" />
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the reply. Yes it is one of the solution. I am wondering if the javascript file i am including in the JSP should also be translated while JSP page translation. This is happenign for javascript functions which are in the JSP file, but not for the JS file.
the js file is not handled by the JSP servlet. Hence it will not have the el expressions evaluated.
I have resolved this by changing the JS file to a JSP (util_js.jsp) file and including the JSP file with <%@ include file="util_js.jsp" %>
1. Set the content type of the JSP in that case. 2. If an answer has worked, on stackoverflow you are invited to mark it as accepted.
2

I do not think you can use EL in javascript file itself. You might be able to use var application_context = ${pageContext.request.contextPath} in your jsp(probably a layout file), while the var application_context itself can be defined directly into the javascript file.

3 Comments

Hi Jinesh Thanks for the answer. Can you elaborate little more with some example.
Here is your test.js var application_context = "www.something.com" Your main.jsp would have <script> application_context = "${pageContext.request.contextPath}". Now in the rest of the js can easily use the application_context variable defined and append the path.
I resolved it by changing the JS file to JSP file. This may not be a better solution, but the way you suggested needs a lot of changes in my code. Thanks for your reply.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.