jQuery offers two powerful methods to execute code and attach event handlers: $(document).ready and $(window).load. The document ready event executes already when the HTML-Document is loaded and the DOM is ready, even if all the graphics haven’t loaded yet. If you want to hook up your events for certain elements before the window loads, then $(document).ready is the right place.
$(document).ready(function() {
// executes when HTML-Document is loaded and DOM is ready
alert("document is ready");
});
The window load event executes a bit later when the complete page is fully loaded, including all frames, objects and images. Therefore functions which concern images or other page contents should be placed in the load event for the window or the content tag itself.
$(window).load(function() {
// executes when complete page is fully loaded, including all frames, objects and images
alert("window is loaded");
});
hello all,
actually i just started with jquery few days ago. problem is that when i attach some events inside $(document).ready function they work nice and fine but when i want to attach a handler outside this function for example $(“#somedi”).click(function myfun(){}); it does not work. i just need to confirm if document.ready is really necessary to attach event handlers and if yes then why?
thanks in advance
You can not attach event handlers before the DOM is loaded. Everything inside the $(document).ready() function will load as soon as the DOM is loaded and before the page contents are loaded. In traditional Javascript you would use the “onload” attribute in the body tag. jQuery offers the $(window).load() function for this, which executes a bit later than $(document).ready().
hello all
and thanks dear for ur kind reply. i have another problem using jquery. i have a table that is dynamically created and loaded onto my page through ajax and now i want to access individual elements of that tables (tr, td) how can i do this in jquery.