Helping ordinary people create extraordinary websites!
GET OUR NEWSLETTER
Your Email:
 

Top 10 Custom JavaScript Functions Of All Time

By Dustin Diaz
2006-08-22


9) addLoadEvent()

Originally written by Simon Willison and highly adopted by many others as a simple way to add events to trigger after the page has loaded. This of course attaches all your events to the onload event handler which some still see as necessary, nevertheless it does exactly what it’s supposed to, and does it well.

addLoadEvent() by Simon Willison

function addLoadEvent(func) {

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

Of course another method is to simply assign multiple event listeners to the window by using addEvent() as described in number 10 as follows:

assigning multiple load events to window

addEvent(window,'load',func1,false);

addEvent(window,'load',func2,false);
addEvent(window,'load',func3,false);


Tutorial Pages:
» Top 10 custom JavaScript functions of all time
» 10) addEvent()
» 9) addLoadEvent()
» 8) getElementsByClass()
» 7) cssQuery()
» 6) toggle()
» 5) insertAfter()
» 4) inArray()
» 3, 2, & 1) getCookie(), setCookie(), deleteCookie()
» Last but not least, a bonus function: The Prototype Dollar Function
» And so will they all…


 | Bookmark
Related Tutorials:
» JavaScript Debugging Techniques with Firebug
» Striped Tables Using JavaScript
» Opening PDFs in a New Window with JavaScript
» Essential Javascript -- A Javascript Tutorial
» Submit Forms Conditionally using JavaScript
» How to Setup a Randomising Function

Advertise with Us!


Tutorials Scripts Web Hosting Developer Manuals
Resources