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?
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?
Share Improve this question edited Nov 18, 2013 at 12:32 user1049057 asked Nov 18, 2013 at 12:07 user1049057user1049057 4594 gold badges15 silver badges37 bronze badges1 Answer
Reset to default 5Write 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
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743781583a4505901.html
评论列表(0条)