?
Solved

$(function () {

Posted on 2013-11-03
6
Medium Priority
?
394 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1000 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 1000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This article discusses how to implement server side field validation and display customized error messages to the client.
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.…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
Suggested Courses

765 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