javascript 如何调用其他jsp页面脚本函数的java脚本函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13619307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to call java script function of other jsp page script function
提问by ankur jadiya
I want to call JavaScript function on index.jsp
from file.jsp
here is my code after getting the value in index.jsp script function then I want to call servlet from inside those function
在获取 index.jsp 脚本函数中的值后,我想index.jsp
从file.jsp
这里调用 JavaScript 函数,然后我想从这些函数内部调用 servlet
this is my index.jsp
这是我的 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>
and this is my file .jsp
这是我的文件 .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>
回答by Yogendra Singh
Perform the steps below:
执行以下步骤:
Move the script in
index.jsp
file to a new JavaScript file e.g.index.js
file and place the file in new folder e.g.js
inside yourWebContent
folder.Link
index.js
inindex.jsp
file by putting below link in the<head>
section:<script language="JavaScript" src="<%=request.getContextPath()%>/js/index.js">
Do the same in
file.jsp
file.
将
index.jsp
文件中的脚本移动到一个新的 JavaScript 文件,例如index.js
文件,并将文件放在新文件夹中,例如js
您的WebContent
文件夹内。链接
index.js
中的index.jsp
文件通过把下面的链接中的<head>
部分:<script language="JavaScript" src="<%=request.getContextPath()%>/js/index.js">
在
file.jsp
文件中做同样的事情。
This way, same javascript code will be available for use in both index.jsp
and file.jsp
files.
这样,相同的 javascript 代码将可用于index.jsp
和file.jsp
文件。