views:

73

answers:

4

We have a JQuery $(function() statement as:

<script type="text/javascript">
$(function(){
  //Code..
})
</script>

Dumb question - when exactly is this function executed? Is it when the entire HTML page has been downloaded by the client?

What is benefit of using the wrapping your code within $(function() as opposed to just doing:

<script type="text/javascript">
//Code..
</script>
+1  A: 

When the document completes loading. It is the same as writing this:

$(document).ready(function(){});

EDIT: To answer your second question:

If you don't wrap your code in the block above then it would fire as soon as it is encountered instead of after all the controls on the page have loaded. So if a block was at the top of a page and it referred to elements in the page those references would not work as the elements have not loaded yet.

But if you wrap in the block then you know that the page has loaded and all elements are available to now reference.

spinon
It actually happens **before** the entire document has been loaded; it's just when the DOM is ready.
VoteyDisciple
@Votey you are right. I should have been more clear. In most cases that is usually not an issue but there can be times when this is an important distinction.
spinon
+3  A: 

It fires when the document has been parsed and is ready, and is the equivalent of $(document).ready(function () { }).

The obvious benefit is that having your script tag before other elements on the page means that your script can interact with them even though they're not available at parse time. If you run your script before elements have been parsed and the document is not ready, they will not be available for interaction.

Andy E
+2  A: 

It is executed as soon as the DOM is parsed and is invoked in order of appearance if there are multiple appearances. At this point the document is however not displayed, its just parsed.

Nrj
I'm not sure that's correct about the document being parsed but not displayed. I thought that browsers generally displayed elements as they're being parsed. Am I mistaken?
patrick dw
@patrick dw: you are not. Also, you can read and manipulate style and position data from `$.ready`; the browser has already calculated the layout of the page at that point.
Tgr
The structure is parsed(may include layout calculations) by browser, but the UI is not yet painted, ie, visible to user.. it is to follow..
Nrj
The UI may or may not be painted at this point - it depends on the browser. All you can say for certain is that the DOM is fully built, but the window OnLoad event has not yet fired, because the browser is still downloading external resources such as images.
Joel Mueller
The UI may very well be painted at this point, in most browsers. Consider script elements that we place at the bottom of the document so that they don't stop the elements before them from being rendered. Any elements after said script will be parsed after the script has finished executing, but elements up until that point should have rendered. @patrick, @Nrj.
Andy E
@Andy E - That's what was making me wonder about that statement in this answer. As you pointed out, the whole purpose of placing scripts at the end of the document (yet often still inside the `<body>`) is that it is a remedy for the fact that javascript *blocks* rendering. It wouldn't be much of a fix if the display didn't happen anyway until the entire document is loaded. :o)
patrick dw
@patrick: Exactly right. It would be pretty ridiculous if browsers didn't support progressive rendering in this day and age. Netscape Navigator supported it in version 1 :-)
Andy E
+1  A: 

It fires after the the document has fully loaded, the DOM tree has been initialized, all CSS styles have been applied and all Javascript has been executed. It differs from the load event in that elements (other than CSS/JS) that load their content from other URLs, such as images or flash files, have not necessarily finished loading at this point. This is usually called the "domready" or "domloaded" event, and some modern browsers support it directly (e.g. Firefox has a DomContentLoaded event), and on others it can be simulated with various tricks, like using the defer attribute or placing a script at the very end of the body.

The advantage is that you can reliably interact with the document at this time; for example you can set an event handler on an element with a certain ID and be sure that it already exists in the DOM tree. On the other hand, it can run considerably earlier than the load event, if some external resource is slow to load. If your script is at the end of your HTML code, then there might be little difference in using or not using the domready event, but usually scripts are called from the head tag, and at that point no elements of the body are available yet.

Tgr