将某个函数放在window.onload事件中最先执行

By | November 18, 2008

如果在window onload事件中,想让某个函数最先执行,该怎么办呢?方法如下:

function addLoadEvent(func)
{
    var oldonload = window.onload;
    if (typeof window.onload != 'function')
    {
        window.onload = func;
    }
    else
    {
        window.onload = function()
        {
            oldonload();
            func();
        }
    }
}