Solved

$(function () {

Posted on 2013-11-03
6
375 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 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help with this homework problem. 5 62
HTML question 2 26
Calculating percentage 2 31
Problem in javascript 17 28
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…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

726 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