开发者

How to call Onload function in the Main page as well as @include file inner.jsp page

I have two pages one is the main page and the another on开发者_运维问答e 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.


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>


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>


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.


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>


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜