473,473 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

onLoad question

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?
Jul 23 '05 #1
5 3373
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()">

Jul 23 '05 #2
judy wrote:
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

Jul 23 '05 #3
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).

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #4
On 22 Nov 2004 07:05:22 GMT, Rob B <fe******@hotmail.com> wrote:

[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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #5


http://www.sitepoint.com/blog-post-view.php?id=171578

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: brian | last post by:
question about Onload function can one define more than one function for Onload ? <body Onload="function1()"> or can one define multiple functions one after another like <body Onload="f() g()...
3
by: Raphael Gluck | last post by:
Hi Everyone Just a simple question If i have more than one Javascript in the head tag, and both have a body Onload how do i write the opening body tag? do i enter two Onloads? This didnt work for...
4
by: Viken Karaguesian | last post by:
Hello all, I have a question. I trying to run two Javascripts on my webpage, both of which need a <body onload> command. They are as follows: <body onload="runMe()"> <body...
3
by: Liming | last post by:
Hi, Here is my situation. I have a textarea on the page. When the page loads, I need it to have some default text (which will be generated dynamically) so I did something like this ...
3
by: Frances | last post by:
I have three functions I need triggered when page loads, so have <body onload="function1();function2();function3()"> but I want to take all these function calls out of body tag and call them...
3
by: John | last post by:
Hi, This is my first experiment with C#, I'm trying to handle the resize event. According to the documentation I should handle the Layout event for this. My question is: how do I register this...
1
by: Ben | last post by:
I have a question about onload() with PHP. my <body> tag in a file call "header.php" my files construct like this. header.php <body> <table>...
7
by: Q | last post by:
Question: why doesn't: window.onLoad = recalculate(); work, and do I have to use: window.onLoad = recalculate; (IE) ???
4
by: mport | last post by:
Does anyone know of possible reasons why onload might not be working? It's gotta be the most pathetic question ever, it's doing my head in!! I've done this a dozen times, but on this one, nothing! I...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.