2

I am trying to clean up an application which is written in jsp servlet and javascript, jquery. Currently in the jsp's, there are lots of javascripts written. I am trying to move all these javascripts to a separate js file. In the jsp, it's getting the contextPath through javascript and then doing some logic using this contextPath. Is it possible to get the context path in the javascript ? Also it's using some code like this in jsp..How can I move this code to js file?

HTML

 <input type="button" id="cntButton" value="CONTINUE" onclick="javascript: callTest('<%=request.getContextPath()%>/test1/test2/test3.do');"/>

 function callTest(pageURL){
 }

If I move function callTest(pageURL) to a javascript file, how can I pass the contextPath from the jsp?

Update 2

In the jsp,

<%
String firstName= "";

if (sampleBean.getName() != null) {
   firstName = sampleBean.getName();
}
%>
<script type="text/javascript">
 window.onload=loadVal;

function loadVal() {    
document.getElementById('business_name').value = "<%=firstName%>";
}
</script>

I need to move this loadVal() to another js file. So how can I pass this "<%=firstName%>" which is fetched from the bean?

1 Answer 1

5

Write this in your page using JSP and refer in js files. It will work.

Make sure you add js file after this code

<script type="text/javascript">

var contextPath='<%=request.getContextPath()%>';

console.log(contextPath);


</script>

and

<input type="button" id="cntButton" value="CONTINUE" 
       onclick="javascript:callTest(contextPath+'/test1/test2/test3.do')" />

or pass url and add it in callTest function

<input type="button" id="cntButton" value="CONTINUE" 
       onclick="javascript:callTest('/test1/test2/test3.do')" />

main.js

function callTest(pageURL)
{
    var url=contextPath+pageURL; 
    //contextPath will be available, if defined in JSP
}
Sign up to request clarification or add additional context in comments.

5 Comments

Hi Murali..var contextPath is already set in my jsp.I just wanted to know, if I move my js function which is currently in the same jsp to a different file, will there be any problem? can I call like $('#cntButton).click(callTest('contextPath/test1/test2/test3.do')); and then write function callTestpageURL(){....}
@user1049057, No you cant. calling callTest('contextPath/test1/test2/test3.do') this passes the url with hardcoded string. Its better to call callTest(contextPath+'/test1/test2/test3.do') or prefix contextPath inside the in callTest function
Ok..So I can write $('#cntButton).click(callTest(contextPath+'/test1/test2/test3.do')); and then write function callTestpageURL(){....}
@user1049057, Yes you can do in that way. No issues. Make sure you defined the variable contextPath and added the .js files in page, for resovling variable scope
Thanks Murali..Thanks for the solution..I have one more query..I have updated the question..can u please help me in this also?

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.