How to add additional event handler to window.onload event?

For example you have following function:

 

function InitCustomCode()

{

    // Your custom initialization

}   

 

and you need to run it, when page loaded.

You can not just write:

<script>

    InitCustomCode();

</script>

 

because it is going to crash Internet Explorer.So you need to add your function to window.onload event handler. However window.onload might be already taken by some other function and you don't have access to this defenition. Then you can use function below to add you handler and maintain already assigned function.

 

function addEvent(obj, evType, fn)

{

    if (obj.addEventListener)

    {

        obj.addEventListener(evType, fn, false);

        return true;

    }

    else

        if (obj.attachEvent)

    {

        var r = obj.attachEvent("on" + evType, fn);

        return r;

    }

    else

    {

        return false;

    }

}

 


Posted on Friday, March 21, 2008 by | Add Comment

New Comment

Your Name:
Email (for internal use only):
Comment:
 
Code above:

Categories

Recent Tweets

  • Simon Ince's Blog: Hierarchies with HierarchyID in SQL 2008 http://t.co/xSDwiF6rRS.
  • Visual Studio 2010 WAS painfully slow - CodeProject http://t.co/Usba1x6CZy

Valid HTML5