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.
--
Randy
comp.lang.javascript FAQ -
http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices -
http://www.JavascriptToolbox.com/bestpractices/