How to access ${pageContext.request.contextPath} of jsp into a external javascript file ?
1 Answer
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>
5 Comments
Hayi
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 definedTodd
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 + "/";Hayi
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.
Hayi
but it seems that
if (typeof contextPath !== 'undefined') works.Hayi
no it's fine now it seems that
if (typeof contextPath !== 'undefined') catch the exception.