Solved

$(function () {

Posted on 2013-11-03
6
364 Views
Last Modified: 2013-11-19
A function that in Javascript starts like this:
$(function () {

How does it work and how do I call it?
0
Comment
Question by:mdoland
6 Comments
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 39620791
Hi

It is well described here

Suggesting to go through w3schools tutorials

Regards
Raj
0
 
LVL 14

Assisted Solution

by:dejaanbu
dejaanbu earned 250 total points
ID: 39620793
$(function() { });     is an jquery alternative to

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

Details here , also

its get called automatically once the page is loaded with necessary images and scripts. if you want to call your own function , you can define that as a separate function and call when needed

http://www.jquery4u.com/jquery-functions/5-ways-declare-functions-jquery/
0
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 250 total points
ID: 39620878
From your tags I noticed that you didn't add jQuery.
What you're asking is a 100% jQuery thing that unifies the way to execute something only when the whole DOM is parsed by the browser.
Different browsers require different approaches to this, and jQuery does a good job unifying all of them.

If you're curious how it does it have a look at the bindReady function code, line 406: https://github.com/jquery/jquery/blob/c8c32f1d0583711355c593fb4c84332bfba18254/speed/jquery-basis.js

Here's the code anyway:
        bindReady: function() {
                if ( readyBound ) {
                        return;
                }

                readyBound = true;

                // Catch cases where $(document).ready() is called after the
                // browser event has already occurred.
                if ( document.readyState === "complete" ) {
                        return jQuery.ready();
                }

                // Mozilla, Opera and webkit nightlies currently support this event
                if ( document.addEventListener ) {
                        // Use the handy event callback
                        document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
                        
                        // A fallback to window.onload, that will always work
                        window.addEventListener( "load", jQuery.ready, false );

                // If IE event model is used
                } else if ( document.attachEvent ) {
                        // ensure firing before onload,
                        // maybe late but safe also for iframes
                        document.attachEvent("onreadystatechange", DOMContentLoaded);
                        
                        // A fallback to window.onload, that will always work
                        window.attachEvent( "onload", jQuery.ready );

                        // If IE and not a frame
                        // continually check to see if the document is ready
                        var toplevel = false;

                        try {
                                toplevel = window.frameElement == null;
                        } catch(e) {}

                        if ( document.documentElement.doScroll && toplevel ) {
                                doScrollCheck();
                        }
                }
        },

Open in new window

I don't want to get too much off topic here, just want to say that using this "DOM Ready" approach is usually unnecessary. If you design your page well, you'll put all the javascript that requires interaction with the DOM right before the </body>. This is the standard, cross browser and trick free way to make sure the DOM is parsed when you run the scripts.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 9

Expert Comment

by:Derek Jensen
ID: 39622417
Indeed @Alex; however I've written code before where I had a jQuery AJAX call that prompted the return of yet another jQuery AJAX call that had to be executed immediately upon DOM update, so...it really just depends on the application. :-)
0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 39623022
I had a jQuery AJAX call that prompted the return of yet another jQuery AJAX call that had to be executed immediately upon DOM update
If you implemented this with $(document).ready(...), calling the exact same callback function at the end of the body would have resulted in the same thing.

Putting the javascript (references and calls) at the end not only avoid the need to use tricks or libraries to properly handle the DOM Ready event, it also improves the user experience by showing the HTML to the user without any delays.
0
 

Author Closing Comment

by:mdoland
ID: 39658717
Perfect!
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now