<p>In my ASP.NET User Control I'm adding some JavaScript to the <code>window.onload</code> event:</p>

<pre><code>if (!Page.ClientScript.IsStartupScriptRegistered(this.GetType(), onloadScriptName))
  Page.ClientScript.RegisterStartupScript(this.GetType(), onloadScriptName, 
    "window.onload = function() {myFunction();};", true);            
</code></pre>

<p>My problem is, if there is already something in the <code>onload</code> event, than this overwrites it. How would I go about allowing two user controls to each execute JavaScript in the <code>onload</code> event?</p>

<p><strong>Edit:</strong> Thanks for the info on third party libraries. I'll keep them in mind. </p>

[{'answer_id': 9440, 'author': 'Derek Park', 'author_id': 872, 'author_profile': 'https://Stackoverflow.com/users/872', 'pm_score': 1, 'selected': False, 'text': "<p>I don't know a lot about ASP.NET, but why not write a custom function for the onload event that in turn calls both functions for you?  If you've got two functions, call them both from a third script which you register for the event.</p>\n"}, {'answer_id': 9447, 'author': 'Chris Farmer', 'author_id': 404, 'author_profile': 'https://Stackoverflow.com/users/404', 'pm_score': 2, 'selected': False, 'text': '<p>Try this:</p>\n\n<pre><code>window.attachEvent("onload", myOtherFunctionToCall);\n\nfunction myOtherFunctionToCall() {\n    // do something\n}\n</code></pre>\n\n<p>edit:  hey, I was just getting ready to log in with Firefox and reformat this myself!  Still doesn\'t seem to format code for me with IE7.</p>\n'}, {'answer_id': 9451, 'author': 'Derek Park', 'author_id': 872, 'author_profile': 'https://Stackoverflow.com/users/872', 'pm_score': 1, 'selected': False, 'text': '<p>Actually, according to <a href="http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx" rel="nofollow noreferrer">this MSDN page</a>, it looks like you <em>can</em> call this function multiple times to register multiple scripts.  You just need to use different keys (the second argument).</p>\n\n<pre><code>Page.ClientScript.RegisterStartupScript(\n    this.GetType(), key1, function1, true);\n\nPage.ClientScript.RegisterStartupScript(\n    this.GetType(), key2, function2, true);\n</code></pre>\n\n<p>I believe that should work.</p>\n'}, {'answer_id': 9470, 'author': 'cole', 'author_id': 910, 'author_profile': 'https://Stackoverflow.com/users/910', 'pm_score': 2, 'selected': False, 'text': '<p><a href="http://mootools.net/" rel="nofollow noreferrer">Mootools</a> is another great JavaScript framework which is fairly easy to use, and like RedWolves said with <a href="http://jquery.com/" rel="nofollow noreferrer">jQuery</a> you can can just keep chucking as many handlers as you want.</p>\n\n<p>For every *.js file I include I just wrap the code in a function.</p>\n\n<pre><code>window.addEvent(\'domready\', function(){\n    alert(\'Just put all your code here\');\n});\n</code></pre>\n\n<p>And there are also <a href="http://walkthrough.ifupdown.com/walkthrough-1.2/domready." rel="nofollow noreferrer">advantages of using domready instead of onload</a></p>\n'}, {'answer_id': 9814, 'author': 'Vincent Robert', 'author_id': 268, 'author_profile': 'https://Stackoverflow.com/users/268', 'pm_score': 4, 'selected': False, 'text': '<p>There still is an ugly solution (which is far inferior to using a framework or <code>addEventListener</code>/<code>attachEvent</code>) that is to save the current <code>onload</code> event:</p>\n\n<pre><code>function addOnLoad(fn)\n{ \n   var old = window.onload;\n   window.onload = function()\n   {\n       old();\n       fn();\n   };\n}\n\naddOnLoad(function()\n{\n   // your code here\n});\naddOnLoad(function()\n{\n   // your code here\n});\naddOnLoad(function()\n{\n   // your code here\n});\n</code></pre>\n\n<p>Note that frameworks like jQuery will provide a way to execute code when the DOM is ready and not when the page loads. </p>\n\n<p>DOM being ready means that your HTML has loaded but not external components like images or stylesheets, allowing you to be called long before the load event fires.</p>\n'}, {'answer_id': 688199, 'author': 'user83421', 'author_id': 83421, 'author_profile': 'https://Stackoverflow.com/users/83421', 'pm_score': 8, 'selected': True, 'text': '<p>Most of the "solutions" suggested are Microsoft-specific, or require bloated libraries. Here\'s one good way. This works with W3C-compliant browsers and with Microsoft IE.</p>\n\n<pre><code>if (window.addEventListener) // W3C standard\n{\n  window.addEventListener(\'load\', myFunction, false); // NB **not** \'onload\'\n} \nelse if (window.attachEvent) // Microsoft\n{\n  window.attachEvent(\'onload\', myFunction);\n}\n</code></pre>\n'}, {'answer_id': 4320764, 'author': 'Fábio de Lima Souto', 'author_id': 495410, 'author_profile': 'https://Stackoverflow.com/users/495410', 'pm_score': 3, 'selected': False, 'text': '<p>I had a similar problem today so I solved it having an <code>index.js</code> with the following:</p>\n\n<pre><code>window.onloadFuncs = [];\n\nwindow.onload = function()\n{\n for(var i in this.onloadFuncs)\n {\n  this.onloadFuncs[i]();\n }\n}\n</code></pre>\n\n<p>and in additional js files that i want to attach the onload event I just have to do this:</p>\n\n<pre><code>window.onloadFuncs.push(function(){\n // code here\n});\n</code></pre>\n\n<p>I normally use jQuery though, but this time I was restricted to pure js wich forced to use my mind for a while!</p>\n'}, {'answer_id': 12192966, 'author': 'DadViegas', 'author_id': 1354106, 'author_profile': 'https://Stackoverflow.com/users/1354106', 'pm_score': 1, 'selected': False, 'text': '<p>You can do this with jquery </p>\n\n<pre><code>$(window).load(function () {\n    // jQuery functions to initialize after the page has loaded.\n});\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/9434', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/233/']