Sunday, December 23, 2007

Multiple onload event functions

Sometimes it is necessary to use third party JavaScript libraries in your projects. These libraries might use some of the the JavaScript events. As a result, when you pass a new function to event to have it run when, for example page is opened, the event of library gets overwritten. Therefore, the library won't function correctly. This is the case for some of the online mapping software that use client-side scripting such as MapQuest. Include the following function to your Onload event handler:

var LibraryFunction= window.onload;
if (typeof window.onload != 'function')
//Is there an event handler already attached to Onload event?
{
   window.onload = MyFunction();
}
else
{
   window.onload = function()
   {
     LibraryFunction();
     MyFunction();
   }
}

No comments: