onLoad question

L

Lee

judy said:
Hi all,
I need to run two functions from the onLoad event. I prefer not
combine them into a single funciton.

Any suggestions on the onLoad syntax?

The onLoad handler is always a single function, whose body is
the string that you provide as the HTML ONLOAD attribute value.
As in any other function, the body can contain more than one
function call on a single line, separated by semi-colons:

<body onload="dothis();dothat()">
 
F

Frances Del Rio

judy said:
Hi all,
I need to run two functions from the onLoad event. I prefer not
combine them into a single funciton.

Any suggestions on the onLoad syntax?


<body onLoad="function1();function2()">

these functions will be executed when page has finished loading..

HTH.... Frances
 
J

judy

Hi all,
I need to run two functions from the onLoad event. I prefer not
combine them into a single funciton.

Any suggestions on the onLoad syntax?
 
R

Rob B

Guessing at your intent...

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>untitled</title>
<script type="text/javascript">
//<![CDATA[

function add_handler(obj, evt, handler, captures)
{
if (obj.addEventListener)
obj.addEventListener(evt, handler, captures);
else if (obj.attachEvent)
obj.attachEvent('on' + evt, handler);
else
{
var old_handler = obj['on' + evt];
if (null == old_handler)
obj['on' + evt] = handler;
else obj['on' + evt] = function()
{
old_handler();
handler();
}
}
}

//]]>
</script>
</head>
<body>
<script type="text/javascript">
//<![CDATA[

function init()
{
alert('function init called');
}

add_handler(window, 'load', init, false);

//]]>
</script>
<script type="text/javascript">
//<![CDATA[

function init2()
{
alert('init2');
}

add_handler(window, 'load', function(){alert('anonymous function
called')}, false);

//]]>
</script>
</body>
</html>

R. Cornford has a smashing example of multiple assignment, somewhat over
my head (short trip).
 
M

Michael Winter

[snip]
else if (obj.attachEvent)
obj.attachEvent('on' + evt, handler);
else
{
var old_handler = obj['on' + evt];
if (null == old_handler)
obj['on' + evt] = handler;
else obj['on' + evt] = function()
{
old_handler();
handler();
}
}

Whilst simple and functional, it does have a problem. The three methods
(two, above) produce a varying, and sometimes deficient, environment for
the listeners.

A conforming implementation of addEventListener will result in two things:

1) Each listener will be passed an event object as its only argument.
2) The this operator will refer to the current target of the event.

This, I think you'll agree, is a nice situation as it replicates what
you'll find if you add your listeners directly with HTML.

IE's attachEvent does neither, and the only way to make it is to add a
function on every call which, itself, calls the listener providing the
missing information. It's not a nice solution, so I tend to avoid
attachEvent altogether.

Your on<event> solution adds uncertainty. On an initial call where the
first branch is taken, listeners will experience something close to what
they would find with addEventListener (except with IE, where the event
object would still be global). However on subsequent calls, this no longer
happens and the situation becomes much like it would with attachEvent.

The quickest fix is:

var old_handler = obj['on' + evt];
if (null == old_handler)
obj['on' + evt] = handler;
else obj['on' + evt] = function(e)
{
old_handler.call(this, e);
handler.call(this, e);
};

It doesn't guarantee that the listener will be passed an event object, but
at least the this operator will be reliable.

Another improvement would be to perform the concatenation of the 'on'
prefix only once:

if(obj.addEventListener) {
/* ... */
} else {
evt = 'on' + evt;
/* ... */
}

as string operations are relatively expensive.

[snip]
R. Cornford has a smashing example of multiple assignment, somewhat over
my head (short trip).

Mind pointing to it (message id or subject)?

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top