0

I have two jsp files named A.jsp and B.jsp I want to call javascript function of B.jsp from A.jsp Code inside a.jsp is

<%@include file="dbconnection.jsp"%>

<HTML>
<HEAD>
<TITLE>
</TITLE>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

</HEAD>
<BODY>
 <TABLE>
       <%ResultSet rs = statement.executeQuery("select * from something;") ;%>
        <% 
        int counter=0;

         while(rs.next()){ 
            String link=rs.getString(1);
            counter++;
        %>
        <TR>
           <TD>

           <p>    <a href="B.jsp" onclick="displayData(<%=counter%>)"><%=link%></a> </p>
           </TD> 
        </TR>
        <% } %>
 </TABLE>
<div id="myDiv"></div>
</BODY>
</HTML>

Code inside B.jsp is

<%@include file="dbconnection.jsp"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<b> Hello</b>

<script language="JavaScript">

function displayData(ctr) {

switch (counter) {

case 1:
$("p").hide();
<%ResultSet rs = statement.executeQuery("select value from abc;") ;  %>
<% 
int c=0;
 while(rs.next()){ 
    String link=rs.getString(1);
    c++;
%>

<% } %>
$('#myDiv').append('<table><tr><td>');
$('#myDiv').append('<INPUT TYPE="button" VALUE="Back" onClick="window.history.go(-1);">');

 $('#myDiv').append('</td></tr></table>');
break;

case 2:
$("a").hide();
<%ResultSet rs1 = statement.executeQuery("select value from xyz;") ;  %>
<% 
int c1=0;
 while(rs1.next()){ 
    String link=rs1.getString(1);
    c1++;
%>

<% } %>

 break;
case 3:
alert("aaa");
break;

 }
}

</script>

</body>

</html>

How can i call function named displatData(ctr) from A.jsp

I tried <a href="B.jsp" onclick="displayData(<%=counter%>)"><%=link%></a> but this is not working. Can anyone help??

Dimple
  • 111
  • 2
  • 11

2 Answers2

0

The javascript method has to be available at the moment it is called. So you have to extract the method in a own .js-file and reference it on A.jsp or move it to A.jsp.

For example A.jsp:

<%@include file="dbconnection.jsp"%>

<HTML>
<HEAD>
<TITLE>
</TITLE>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript" src="myScripts.js"></script>

</HEAD>
<BODY>
 <TABLE>
       <%ResultSet rs = statement.executeQuery("select * from something;") ;%>
        <% 
        int counter=0;

         while(rs.next()){ 
            String link=rs.getString(1);
            counter++;
        %>
        <TR>
           <TD>

           <p>    <a href="B.jsp" onclick="displayData(<%=counter%>)"><%=link%></a> </p>
           </TD> 
        </TR>
        <% } %>
 </TABLE>
<div id="myDiv"></div>
</BODY>
</HTML>

B.jsp:

<%@include file="dbconnection.jsp"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>
<script type="text/javascript" src="myScripts.js"></script>

</head>

<body>

<b> Hello</b>

</body>

</html>

And finally myScripts.js:

function displayData(ctr) {

switch (counter) {

case 1:
$("p").hide();
<%ResultSet rs = statement.executeQuery("select value from abc;") ;  %>
<% 
int c=0;
 while(rs.next()){ 
    String link=rs.getString(1);
    c++;
%>

<% } %>
$('#myDiv').append('<table><tr><td>');
$('#myDiv').append('<INPUT TYPE="button" VALUE="Back" onClick="window.history.go(-1);">');

 $('#myDiv').append('</td></tr></table>');
break;

case 2:
$("a").hide();
<%ResultSet rs1 = statement.executeQuery("select value from xyz;") ;  %>
<% 
int c1=0;
 while(rs1.next()){ 
    String link=rs1.getString(1);
    c1++;
%>

<% } %>

 break;
case 3:
alert("aaa");
break;

 }
}
  • When i write that script function inside A.jsp then back button is not working – Dimple Mar 30 '15 at 08:30
  • Please see the following problem. It may be yours as well: [link](http://stackoverflow.com/questions/15622100/how-can-i-disable-href-if-onclick-is-executed) – Streetshark Mar 30 '15 at 11:53
0

In A.jsp, instead of using the following link:

<a href="B.jsp" onclick="displayData(<%=counter%>)"><%=link%></a>

You can include the B.jsp on the top using <%@ include file="B.jsp" %> and then use the following :

<a href="javascript:displayData(<%=counter%>)"><%=link%></a>

This should work for you.

war10ck
  • 16
  • 1