In fact, within a single <script> tag, function definitions happen first,
before any code in that <script> tag is executed. It would be perfectly
valid to write:

 <script type="text/javascript">
    // will be run immediately, but *after* callit is defined
    callit();

    // will be defined immediately, and *before* the code above is excuted
    function callit() {
       alert("Hello world!");
    }
 </script>

I'm not saying that this code would be the same as the $(document).ready()
version - it doesn't wait for DOM ready - just pointing out that function
definitions are processed before code is executed.

It works the same way inside a function:

 <script type="text/javascript">
    outer();

    function outer() {
        inner();

        function inner() {
           alert("Hello world!");
        }
    }
 </script>

That code will alert "Hello world!" even though the call to each function
precedes its definition.

-Mike

On Wed, Dec 16, 2009 at 6:17 AM, Richard D. Worth <rdwo...@gmail.com> wrote:

>
> No need to add another block. As you pointed out earlier, the document
> ready won't run until later. By the time it does, the callit function will
> have been defined:
>
>
>  <script type="text/javascript">
>     $(document).ready(function() {
>       // will be run when doc is loaded
>       callit();
>     });
>
>     // will be defined immediately
>     function callit() {
>        alert("Hello world!");
>     }
>  </script>
>
> - Richard
>
>

Reply via email to