$(function () {

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

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

Expert Comment

by:Rajkumar Gs
ID: 39620791

It is well described here

Suggesting to go through w3schools tutorials

LVL 14

Assisted Solution

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
LVL 30

Accepted Solution

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:

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

                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 ) {

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.
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline


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, really just depends on the application. :-)
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.

Author Closing Comment

ID: 39658717

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

707 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

12 Experts available now in Live!

Get 1:1 Help Now