views:

250

answers:

2

Hi,

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"&gt;
<%@ 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.

A: 

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>
BalusC
will it get called when page is loaded?
Salil
Inline scripts will get executed immediately when the browser parses it. That's why it has to be placed at the bottom, *after* the HTML elements where it is supposed to intercept on.
BalusC
+1  A: 

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