Javascript 如何调用其他jsp页面脚本函数的java脚本函数

Javascript 如何调用其他jsp页面脚本函数的java脚本函数,javascript,jsp,Javascript,Jsp,我想从file.jsp调用index.jsp上的JavaScript函数,这是我在index.jsp脚本函数中获取值后的代码,然后我想从这些函数内部调用servlet 这是我的index.jsp <%@page import="org.apache.catalina.connector.Request"%> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO

我想从
file.jsp
调用
index.jsp
上的JavaScript函数,这是我在index.jsp脚本函数中获取值后的代码,然后我想从这些函数内部调用servlet

这是我的index.jsp

<%@page import="org.apache.catalina.connector.Request"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jstl/xml" %>
<html>
    <head>
     <script type="text/javascript">
           function popuponclick()
           {
              var mywindow=window.open("file.jsp", "file","status=1,width=350,height=150");
           }
           function onLoadById(long id)
           {
              after getting value call the servlet GetEmployeeServlet passing id as a parameter
           }
           function onLoadByname(String name)
           {
              after getting value call the servlet GetEmployeeServlet passing name as a parameter
           }    
     </script>
    </head>
    <body>

    <form name="form1">

    <%String name11=request.getParameter("name"); 
    out.println(name11);%>
        <% if(name11!=null){
            out.println(name11);
            session.setAttribute("EmployeeById","1");}
        %>
    <table>
    <tr>
    <td><input type="submit"  onclick="popuponclick()" value="GetEmployeeById" name="name"/>
    <input type="hidden" name="GetEmp" value="1"></td>
    </tr>

    <tr>
    <td><input type="submit"  onclick="popuponclick()" value="GetEmployeeByname" name="name1"></td>
    </tr>
     </table>
     </form>
    </body>
    </html>

函数popuponclick()
{
var mywindow=window.open(“file.jsp”,“file”,“status=1,width=350,height=150”);
}
函数onLoadById(长id)
{
获取值后,调用servlet GetEmployeeServlet,将id作为参数传递
}
函数onLoadByname(字符串名称)
{
获取值后,调用servlet GetEmployeeServlet,将名称作为参数传递
}    
这是我的文件.jsp

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt"%>
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql"%>
<%@ taglib prefix="x" uri="http://java.sun.com/jstl/xml"%>
<%@page
    import="com.nousinfo.tutorial.employee.service.model.bo.EmployeeBO"%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>

<script>
function myfuntion()
{
    var d=document.getElementById('first');
    var c=document.getElementById('second');
    alert(window.parent);
    window.close();
    }
</script>
</head>

<bod>

<% if (session.getAttribute("EmployeeById")!=null) {
    session.removeAttribute("EmployeeById");
%>
<div>
<table>
<tr>
<td>GetEmployeeByName</td>
</tr>
<tr>
<td><input id="first" type="text" name="GetEmployeeByName"/></td></tr>
</table>
</div>
<% } else { %>
<div>
<table>
<tr>
<td>GetEmployeeById</td>
</tr>
<tr>
<td><input id="second" type="text" name="GetEmployeeById"/></td></tr>
</table>
</div>
<% } %>
<table>
<tr>
<td><input id="submit"  type="submit" name="submit" value="find" onclick="myfuntion()"></td>
</tr>
</table>

</body>
</html>

在此处插入标题
函数myfuntion()
{
var d=document.getElementById('first');
var c=document.getElementById('second');
警报(window.parent);
window.close();
}
GetEmployeeByName
GetEmployeeById

执行以下步骤:

  • index.jsp
    文件中的脚本移动到一个新的JavaScript文件,例如
    index.js
    文件,并将该文件放入
    WebContent
    文件夹中的新文件夹,例如
    js

  • index.jsp
    文件中的
    index.js
    链接到
    部分的下面链接:

    <script language="JavaScript" src="<%=request.getContextPath()%>/js/index.js">
    
    
    
  • file.jsp
    文件中执行相同的操作


  • 这样,相同的javascript代码将可在
    index.jsp
    file.jsp
    文件中使用。

    执行以下步骤:

  • index.jsp
    文件中的脚本移动到一个新的JavaScript文件,例如
    index.js
    文件,并将该文件放入
    WebContent
    文件夹中的新文件夹,例如
    js

  • index.jsp
    文件中的
    index.js
    链接到
    部分的下面链接:

    <script language="JavaScript" src="<%=request.getContextPath()%>/js/index.js">
    
    
    
  • file.jsp
    文件中执行相同的操作


  • 这样,相同的javascript代码将可用于
    index.jsp
    file.jsp
    文件中。

    您能告诉我如何使用java在子窗口(file.jsp)中获取index.jsp(父窗口)对象吗svript@ankurjadiya您可以始终使用
    window.parent.document.getElementById(elementId)
    或类似函数来引用子窗口中的父
    DOM
    元素。这里的关键是使用
    window.parent
    引用父窗口。您能告诉我如何使用java在子窗口(file.jsp)中获取index.jsp(父窗口)对象吗svript@ankurjadiya您可以始终使用
    window.parent.document.getElementById(elementId)
    或类似函数来引用子窗口中的父
    DOM
    元素。这里的关键是使用
    window.parent
    引用父级。