Home → Resources → Articles

Generic JavaScript Page onLoad Event Handler

(Web Programming) by Jason Skowronek on 08/18/2009
//setup onload function
if(typeof window.addEventListener != 'undefined')
{
    //.. gecko, safari, konqueror and standard
    window.addEventListener('load', generic, false);
}
else if(typeof document.addEventListener != 'undefined')
{
    //.. opera 
    document.addEventListener('load', generic, false);
}
else if(typeof window.attachEvent != 'undefined')
{
    //.. win/ie
    window.attachEvent('onload', generic);
}

//** remove this condition to degrade older browsers
else
{
    //.. mac/ie5 and anything else that gets this far
    
    //if there's an existing onload function
    if(typeof window.onload == 'function')
    {
        //store it
        var existing = onload;
        
        //add new onload handler
        window.onload = function()
        {
            //call existing onload function
            existing();
            
            //call generic onload function
            generic();
        };
    }
    else
    {
        //setup onload function
        window.onload = generic;
    }
}

As referenced from Brothercake.com

Comments (0)

Leave a comment
Name *
Email *
Homepage
Comment

Recommend

CS5 Web Premium - Download 

System Mechanic 

SkoNet provides comprehensive digital consulting services such as: web development, applications development, database design and architecture, business process management, customer relationship management, and many others that help businesses of every size, industry, and geography meet the complex challenge of managing and sharing information on the web. Our skills and expertise in online systems allow us to help customers build applications ranging from simple, single-page web sites to robust enterprise systems.

Online Backup, Ektron Consulting, Ektron Programmer, Ektron Developer, Ektron Partner Utah, Ektron Partner, Ektron Architect, Ektron Hosting, Salesforce.com Consultant Utah, Salesforce.com Partner Utah, Salesforce.com Partner, Salesforce.com Programmer, Salesforce.com Architect, Salesforce.com APEX Programmer