如何在Main页面以及@include文件inner.jsp页面中调用Onload函数
我有两个页面,一个是主页,另一个是内页:
页面名称: main.jsp , sidebar.jsp 我想在这两个页面上调用 onload 函数。这可能吗。如果是的话怎么办?
下面是 main.jsp 的代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ include file="/pages/common/init.jsp"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>J.C. Taylor - Broker Website</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<link rel="stylesheet" href="/css/default.css" media="screen" type="text/css" />
</head>
<body onload="prepopulateFields();load(17);">
<s:form name="continue" id="continue_id" action="continue" method="POST" validate="true" enctype="multipart/form-data">
<div id="wrapper">
<div id="main">
<div id="sidebar">
<%@ include file="/pages/common/sidebar.jsp"%>
<span class="clearIt"></span>
</div>
sidebar.jsp 是:
<body onload="setSelected();">
//Some static content here
</body>
所以基本上我想要的是调用 prepopulateFields() Javascript 方法,该方法属于主 .jsp 页面的 onload() 事件和 setSelected( ) 分别属于 sidebar.jsp 的 onload() 方法。
我知道我可以调用 setSelected(); prepopulateFields() 方法内的方法,但我不想这样做。我想要的是当页面加载时,两个 onload 函数应该分别调用。
如果您有任何建议,请告诉我! 我知道我在这里有点可笑,但如果我能做到这一点,我的工作就会很容易。
I have two pages one is the main page and the another one is the inner page:
Page names: main.jsp , sidebar.jsp
I want to call the onload function on both of these pages. Is that possible. If yes How?
Below is the code for main.jsp:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ include file="/pages/common/init.jsp"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>J.C. Taylor - Broker Website</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<link rel="stylesheet" href="/css/default.css" media="screen" type="text/css" />
</head>
<body onload="prepopulateFields();load(17);">
<s:form name="continue" id="continue_id" action="continue" method="POST" validate="true" enctype="multipart/form-data">
<div id="wrapper">
<div id="main">
<div id="sidebar">
<%@ include file="/pages/common/sidebar.jsp"%>
<span class="clearIt"></span>
</div>
The sidebar.jsp is:
<body onload="setSelected();">
//Some static content here
</body>
So Basically I want is to call prepopulateFields() Javascript method which belongs to onload() event of the main .jsp page and setSelected() which belongs to onload() method of the sidebar.jsp symulatneously and separately.
I know I can call the setSelected(); method inside the prepopulateFields() method but that I dont want to do. All I want is when the page is loaded both the onload functions should be called separately.
If you have some suggestions please do let me know!
I know I am being little bit ridiculous here but if I could do that My job will be very easy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不认为你可以调用多个 onload 函数。
最好的方法是从已经调用的函数中调用该方法
i don't think you can call more than one onload function.
best way is to call the method from already called function
您不能嵌套 HTML
元素,这只会导致 HTML 格式错误。
最好的办法是将其作为
放在
sidebar.jsp
的bottom 中。You cannot nest HTML
<body>
elements, it would only malform HTML.Best is to put it as a
<script>
at the bottom ofsidebar.jsp
.如果您使用 firebug 检查 main.jsp 渲染的 html 页面。你会看到只有一个< body > 元素。 < sidebar.jsp 中的 body > 元素未呈现,因为它将 HTML 格式错误为 包含的 jsp 中不允许使用 html 或正文。
解决方案是:
If you use firebug to inspect the rendered html page of main.jsp. You would see there is only one < body > element. The < body > element in your sidebar.jsp is not rendered since it will malform HTML as html or body not allowed in included jsp.
The solution is:
window.onload = 代码地址;应该有效。这是一个演示。以及完整的代码:
window.onload = codeAddress; should work. Here's a demo. And the full code:
将类定义放在父 jsp 中,并在包含中实例化所需数量的 onload。
Put the class definition in the parent jsp, and instantiate as many onloads as you need in the includes.