1

How to access ${pageContext.request.contextPath} of jsp into a external javascript file ?

1 Answer 1

4

before the script in which you'll need it, you should be able to include something like this. then access it by variable name.

<script>var myContextPath = "${pageContext.request.contextPath}"</script>
<script src='theScriptINeedContextFor.js'></script>
Sign up to request clarification or add additional context in comments.

5 Comments

it work but when i try to make a test in another js file var prefix = "" ; if( contextPath != undefined ) prefix = contextPath + "/" ; i got this error Uncaught ReferenceError: contextPath is not defined
is this test.js included before or after the contextPath variable is declared? also, you want to check like this if (typeof contextPath !== 'undefined') prefix = contextPath + "/";
no i try it in a other jsp so the contextPath variable is not defined because i don't need it in this jsp page.
but it seems that if (typeof contextPath !== 'undefined') works.
no it's fine now it seems that if (typeof contextPath !== 'undefined') catch the exception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.