This page describes how to call a function or JavaScript code when the page/DOM is ready without using jquery or any other JavaScript library/framework.
After searching the web for a good example of doing this, most roads lead to jQuery, however I really needed to find a way without using an external library. Simply because it would have been an unnecessary overhead as no other items called for the use of jQuery on the page.
Here is the code:
//add event
if (window.addEventListener){
window.addEventListener("load", function(){ alert('DOM Ready!!'); }, false);
}else if
(window.attachEvent){
window.attachEvent("onload", alert('DOM Ready!!'););
}else{
window.onload = alert('DOM Ready!!');
}
You can add JavaScript code directly to the above code or specify a function to call when the DOM has loaded fully. To see a full example of using the JavaScript DOM ready function please view the following link on pastebin.com
To use the jQuery DOM ready function use: $(document).ready(function() { alert('DOM Ready!!') }); for more information see the jQuery website.
This page was last updated: 17 May 2011