如何在Main页面以及@include文件inner.jsp页面中调用Onload函数

发布于 2024-09-04 03:46:44 字数 1590 浏览 4 评论 0原文

我有两个页面,一个是主页,另一个是内页:

页面名称: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

箹锭⒈辈孓 2024-09-11 03:46:44

我不认为你可以调用多个 onload 函数。
最好的方法是从已经调用的函数中调用该方法

function prepopulateFields(){
    if //condition which check the current page where you want other onload function
       setSelected();
}

<body onload="prepopulateFields();load(17);">



</body>

i don't think you can call more than one onload function.
best way is to call the method from already called function

function prepopulateFields(){
    if //condition which check the current page where you want other onload function
       setSelected();
}

<body onload="prepopulateFields();load(17);">



</body>
清欢 2024-09-11 03:46:44

您不能嵌套 HTML 元素,这只会导致 HTML 格式错误。

最好的办法是将其作为

<script type="text/javascript">setSelected()</script>

You cannot nest HTML <body> elements, it would only malform HTML.

Best is to put it as a <script> at the bottom of sidebar.jsp.

<script type="text/javascript">setSelected()</script>
jJeQQOZ5 2024-09-11 03:46:44

如果您使用 firebug 检查 main.jsp 渲染的 html 页面。你会看到只有一个< body > 元素。 < sidebar.jsp 中的 body > 元素未呈现,因为它将 HTML 格式错误为 包含的 jsp 中不允许使用 html 或正文

Be careful that the included file does not contain <html>, </html>, <body>, or </body> tags

解决方案是:

  1. 如果 sidebar.jsp 始终加载,则将 setSelected() 放入 main.jsp 正文 onload 事件中;
  2. 或按照 BalusC 的建议进行操作。

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.

Be careful that the included file does not contain <html>, </html>, <body>, or </body> tags

The solution is:

  1. either put your setSelected() into main.jsp body onload event if the sidebar.jsp is always loaded;
  2. or do as BalusC suggested.
反话 2024-09-11 03:46:44

window.onload = 代码地址;应该有效。这是一个演示。以及完整的代码:

<script type="text/javascript">
function codeAddress() {
    alert('ok');
}
window.onload = codeAddress;
</script>

window.onload = codeAddress; should work. Here's a demo. And the full code:

<script type="text/javascript">
function codeAddress() {
    alert('ok');
}
window.onload = codeAddress;
</script>
愿得七秒忆 2024-09-11 03:46:44

将类定义放在父 jsp 中,并在包含中实例化所需数量的 onload。

<SCRIPT>
    // this portion was placed above the Class definition for convenience.
    // make sure the Class definition gets loaded first in your code.

    new OnLoad(function(){
        alert("document loaded");
    },100);

</SCRIPT>
...
<SCRIPT>
        // Class Definition
        OnLoad = function(taskFunction,miliseconds) { 
            var context = this;
            context.cnt = 0;
            context.id = null;
            context.doTask=taskFunction;
            context.interval = function() {
                if(document.readyState == "complete"){
                    try{   context.stop();} catch(e){ throw new Error("stop error: " + context.id); }
                    try{ context.doTask();} catch(e){ throw new Error("load error: " + context.id); }
                }   
            };
            context.start = function(timing) {
               if(context.id && context.id!=null)
                   context.stop();
               context.cnt=0;
               context.id=setInterval(context.interval,timing);
            };
            context.stop = function() {
               var _id = context.id;
               clearInterval(context.id);
               context.id=null;
            };
            context.start(miliseconds ? miliseconds : 100);
        };
   </SCRIPT>

Put the class definition in the parent jsp, and instantiate as many onloads as you need in the includes.

<SCRIPT>
    // this portion was placed above the Class definition for convenience.
    // make sure the Class definition gets loaded first in your code.

    new OnLoad(function(){
        alert("document loaded");
    },100);

</SCRIPT>
...
<SCRIPT>
        // Class Definition
        OnLoad = function(taskFunction,miliseconds) { 
            var context = this;
            context.cnt = 0;
            context.id = null;
            context.doTask=taskFunction;
            context.interval = function() {
                if(document.readyState == "complete"){
                    try{   context.stop();} catch(e){ throw new Error("stop error: " + context.id); }
                    try{ context.doTask();} catch(e){ throw new Error("load error: " + context.id); }
                }   
            };
            context.start = function(timing) {
               if(context.id && context.id!=null)
                   context.stop();
               context.cnt=0;
               context.id=setInterval(context.interval,timing);
            };
            context.stop = function() {
               var _id = context.id;
               clearInterval(context.id);
               context.id=null;
            };
            context.start(miliseconds ? miliseconds : 100);
        };
   </SCRIPT>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文