javascript - 如何调用其他jsp页面脚本函数的javascript函数

标签 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-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>

这是我的文件 .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>

最佳答案

执行以下步骤:

  1. 将脚本移至 index.jsp文件到一个新的 JavaScript 文件,例如index.js文件并将文件放置在新文件夹中,例如js在你的WebContent里面文件夹。

  2. 链接 index.jsindex.jsp通过将以下链接放入 <head> 中来创建文件部分:

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

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

关于javascript - 如何调用其他jsp页面脚本函数的javascript函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13619307/

相关文章:

java - JSP 页面上的脚本,用于检查数据库中的值

尝试执行 JSTL < c :if > condition 时出现 java.lang.IllegalArgumentException

java - 为什么我无法访问这个放入 Model 对象的列表到这个 Spring MVC 项目中?

javascript - 为列表框中的选项获取 "InvalidCharacterError: String contains an invalid character"

javascript - 使用JS从icalendar文件中读取参数

javascript - 需要帮助来安装 BxSlider

javascript - 使用 onClick javascript 将类添加到 span 元素

javascript - 如何在数组中向网络工作人员创建/发布消息

java - 在文件夹下的文件中搜索有效模式? (也许使用 Perl 或使用 Java 的一些 API 或其他任何东西)

javascript - Promise 后的火灾事件