Google Answers Logo
View Question
Q: Appending to event functions in javascript. ( No Answer,   3 Comments )
Subject: Appending to event functions in javascript.
Category: Computers > Programming
Asked by: joepicc-ga
List Price: $2.00
Posted: 18 Apr 2005 13:54 PDT
Expires: 18 May 2005 13:54 PDT
Question ID: 510976
I have this code.

<body onload="funcSetByHTML();nFunctionsIDontKnowWhatTheyWillBe()">
later in javascript i want to add javascript like
document.body.onload= "funcSetByJS()"

How can use the document.body.onload="" code without killing the code
I set in the html? I want the onload to look like this when i am done.
onload="funcSetByHTML(); nFunctionsIDontKnowWhatTheyWillBe();

I need a way to append to the onload event, rather then rest it? Can this be done?
There is no answer at this time.

Subject: Re: Appending to event functions in javascript.
From: bigdoggy-ga on 19 Apr 2005 07:04 PDT
Sounds like you might be going at it the hard way.  What you're saying
is that you want to add functionA() if a certain condition exists and
functionB() if another condition exists and do nothing if neither

Instead of doing it that way, why not just call a function every time
that determines what to do?

onload="funcSetByHTML(); nFunctionsIDontKnowWhatTheyWillBe();

function whatShouldIDo()
  { functionA() }
 else if(condition2)
  { functionB() }

That should eliminate the whole reset problem.
Subject: Re: Appending to event functions in javascript.
From: willcodeforfood-ga on 19 Apr 2005 09:10 PDT
This is actually very easy to do.  Here's an example that shows how to
do what you are asking:

	function originalOnLoad()
		alert("original onLoad called")
<body onLoad="originalOnLoad()">
	Demo of onLoad hook.

	function addedOnLoad()
		if (origOL)
		alert("added onLoad called")
	var origOL = document.onLoad
	document.onLoad = addedOnLoad()
Subject: Re: Appending to event functions in javascript.
From: ivankara-ga on 10 Apr 2006 16:44 PDT
It doesn't look like this ever got answered, but the following code
ought to work. It just adds an event listener, which there can be an
arbitrary number of.

function addListener(element, event, listener, bubble) {
  if(element.addEventListener) {
    if(typeof(bubble) == "undefined") bubble = false;
    element.addEventListener(event, listener, bubble);
  } else if(this.attachEvent) {
    element.attachEvent("on" + event, listener);

addListener(this, "load", function() { myFunction(); });
addListener(document, "load", function() { myFunction(); });

Adding event listeners varies by browser, thus the function to do so.
It seems as though both IE and Mozilla want the event attached to
"this," but I'm thinking when I originially wrote this code, I was
also attaching it to document for a reason. Perhaps not...

<a href="">The
original test.</a>

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  

Google Home - Answers FAQ - Terms of Service - Privacy Policy