Forum Moderators: open

Message Too Old, No Replies

How to load an Javascript function on Body onload for the code below

         

srins

7:04 pm on Dec 4, 2006 (gmt 0)

10+ Year Member



Hi ,

I tried to load one javascript method using

<script src="usableforms.js"></script>

which has prepareForm method in this file.
<code>
<body onload="init()" topmargin=0> also tried

<body onLoad="javascript:prepareForm();" topmargin=0 >
</code>
which calls
<code>
<html:html locale="true">
<head>
<script src="usableforms.js"></script>
<script>
function init()
{
prepareForm();
}

</code>

basically this function is used,

when i check an default check box,ie
<code>
<input type="checkbox" name="capability" show="Default" value="default" >Default
</code>
which has show tag in html which matches with relation in table tr as Default given as below,and expand this input text when i check capability check box.
<code>
<tr relation="Default">
<td > Default Value</td>
<td >
<html:text property="defa"/>
</td>
</tr>

But the prepare form which i am calling is not loaded at onLoad.

I have many function and kept in . js file ,here i am copying the Prepare method alone.

<code>
function prepareForm()
{
if (!compatible) return;
var marker = document.createElement(relatedTag);
marker.style.display = 'none';

var x = document.getElementsByTagName(relatedTag);
var toBeRemoved = new Array;
for (var i=0;i<x.length;i++)
{
if (x.getAttribute('relation'))
{
var y = getAllFormFields(x);
x.nestedRels = new Array;
for (var j=0;j<y.length;j++)
{
var rel = y[j].getAttribute('show');
if (!rel ¦¦ rel == 'none') continue;
x.nestedRels.push(rel);
if (y.checked && y.getAttribute('show'))
intoMainForm(y.getAttribute('show'))
}

var z = document.getElementsByTagName('select');

// Opera weird with hidden selects in quirks mode: selectedIndex = -1

for (var i=0;i<z.length;i++)
{
if (z.options[z.selectedIndex].getAttribute('show'))
{
z.onchange = arrangeFormFields;
intoMainForm(z.options[z.selectedIndex].getAttribute('show'))
}
}
}

></code>

Can any one help me in this regard on how to load this method on onload method.

But the Same Java script was working fine on body onload through Perl/CGI programming page,but finding some issue when i do with JSP page.

Thanks,
Srins.

Fotiman

3:59 pm on Dec 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



1. You should be separating content, presentation, and behaviors. So avoid inline event handlers like onload and avoid presentational attributes like topmargin. Your body tag should look like this:

<body>

and then you can give it margin using CSS.

<style type="text/css">
body { margin-top: 0; }
</style>

2. To assign your onload event handler, you could do something like this:

<script type="text/javascript" src="usableforms.js"></script>
<script type="text/javascript">
window.onload = init;
</script>

That assumes that usableforms.js has an init method defined.

Hope that helps.