473,513 Members | 2,684 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

calling onload twice

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
Nov 23 '05 #1
3 5266
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;

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Nov 23 '05 #2
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/
Nov 23 '05 #3
On 2005-11-20, Andrew Poulos <ap*****@hotmail.com> wrote:
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


save_onload=window.onload
window.onload=function(){ save_onload(); YOUR CODE HERE }

there may be a way to do it without polluting the name space (with
save_onload) but I haven't figured it out.

--

Bye.
Jasen
Nov 23 '05 #4

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

Similar topics

14
6378
by: Axel Straschil | last post by:
Hello! Im working with new (object) classes and normaly call init of ther motherclass with callin super(...), workes fine. No, I've got a case with multiple inherance and want to ask if this...
4
5002
by: Joe Kehnast | last post by:
I'm running a system on W2K web server with a Professional XP client. I'm opening a window from an asp page using showModalDialog. In the window I open I'm calling the onLoad event from the body...
1
5812
by: Lewis | last post by:
Hi, Im having a problem with an aspx page that I'm building (and rather urgently needing to complete!). Situation is this... Im transforming some XML using an XSLT which builds an HTML form...
0
1227
by: RAJ | last post by:
This event appears to be invoked twice for absolutely no reason on only one of my ASP.Net web user controls. Although I've now overcome the problem by overriding the base classes OnLoad routine, I...
2
1459
by: RAJ | last post by:
Posting this thread again, can somebody please help me with this. Thanks. This event appears to be invoked twice for absolutely no reason on only one of my ASP.Net web user controls. Although...
14
13092
by: V. Jenks | last post by:
I'm a little rusty having not touched .NET for 6 months and I can't remember why Page_Load is happening twice in this code: private void Page_Load(object sender, System.EventArgs e) {...
4
2803
by: Adam Smith | last post by:
Hello, How can I call or trigger an external javascript twice in a form? I have <script language="JavaScript" src="country_state.js" name="Country_State"> <script type="text/javascript"...
6
19229
by: Daz | last post by:
Hello everyone, I would like to open a child window from the parent, and add an onload event listener to the child window which will tell the parent when the document has loaded. As far as I...
20
11832
by: Mark Anderson | last post by:
Hi, I have this in an external JS library: ///////////////////////// function addMyEvent(){ var obj; if(document.attachEvent) { obj = document.getElementsByTagName('img'); for...
0
7379
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,...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7521
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5084
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
4745
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
3232
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
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1591
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 ...
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.