calling onload twice

A

Andrew Poulos

I've been asked to add some additional code to HTML that get generated
by a 3rd party program. Currently the code that gets generated already
includes a window.onload = init;

The program lets me add additional 'custom' code but it simply adds it
to the end of the SCRIPT tag. This is fine, except that I need my code
to execute onload. Is there a way to allow the existing window.onload to
run but to also include/add mine?

Andrew Poulos
 
E

Evertjan.

Andrew Poulos wrote on 20 nov 2005 in comp.lang.javascript:
I've been asked to add some additional code to HTML that get generated
by a 3rd party program. Currently the code that gets generated already
includes a window.onload = init;

The program lets me add additional 'custom' code but it simply adds it
to the end of the SCRIPT tag. This is fine, except that I need my code
to execute onload. Is there a way to allow the existing window.onload to
run but to also include/add mine?

function init2(){alert(1);init();alert(2)};

window.onload = init2;
 
R

Randy Webb

Andrew Poulos said the following on 11/20/2005 3:36 PM:
I've been asked to add some additional code to HTML that get generated
by a 3rd party program. Currently the code that gets generated already
includes a window.onload = init;

The program lets me add additional 'custom' code but it simply adds it
to the end of the SCRIPT tag. This is fine, except that I need my code
to execute onload. Is there a way to allow the existing window.onload to
run but to also include/add mine?

function init(){
alert('inside init')
}
window.onload = init;

//above is the original code (or something similar)

//save a reference to the original onload
window.oldOnload = window.onload;

//add a new onload to a custom function.
window.onload = newOnload;

function newOnload(){
//call the original onload
if (window.oldOnload){
window.oldOnload();
}
//call your new onload function
init2();
}

//your onload function
function init2(){
alert('inside init2')
}

The above doesn't work if the onload is called via the body tag.
The above still works even if the original code is not present. Meaning,
you can start at just the window.oldUnload and it will still execute
without errors.
 
L

Lee

Andrew Poulos said:
I've been asked to add some additional code to HTML that get generated
by a 3rd party program. Currently the code that gets generated already
includes a window.onload = init;

The program lets me add additional 'custom' code but it simply adds it
to the end of the SCRIPT tag. This is fine, except that I need my code
to execute onload. Is there a way to allow the existing window.onload to
run but to also include/add mine?

window.onload=mycode;

function mycode() {
init();
// your stuff;
}
 

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,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top