Solved

$(function () {

Posted on 2013-11-03
6
359 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

895 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

15 Experts available now in Live!

Get 1:1 Help Now