Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
-3
rated 0 times [  3] [ 6]  / answers: 1 / hits: 33968  / 11 Years ago, mon, november 18, 2013, 12:00:00

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?


More From » jquery

 Answers
8

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
}

[#74227] Saturday, November 16, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
marisela

Total Points: 103
Total Questions: 105
Total Answers: 102

Location: Solomon Islands
Member since Fri, Oct 8, 2021
3 Years ago
;