Solved

is $(document).ready(function() the same as     $(function () {

Posted on 2014-07-23
9
571 Views
Last Modified: 2014-07-27
In JQuery,

is $(document).ready(function() the same as     $(function () {   }
0
Comment
Question by:brgdotnet
[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
  • 4
  • 3
9 Comments
 
LVL 13

Expert Comment

by:duncanb7
ID: 40216088
There is different between

At $(document).ready(function(),  
The function() will operate after the whole page is completely loaded and DOM is ready.
But
$(function () {   }
is self-start or self calling anonymous function that is just function, it will operate or start as soon as possible when the browser is interpreting your ecma-/javascript  regardless the page being loaded completely or not

Duncan
0
 
LVL 53

Accepted Solution

by:
Scott Fell,  EE MVE earned 160 total points
ID: 40216154
These two sets of code below are the same http://api.jquery.com/ready/.  I personally use the later because it is faster to type out.
$( document ).ready(function() {
  // Handler for .ready() called.
});

Open in new window

$(function() {
  // Handler for .ready() called.
});

Open in new window

0
 
LVL 53

Expert Comment

by:Scott Fell, EE MVE
ID: 40216250
@duncanb7, I'm just trying to double check because we seem to have different answers for the same thing.  

self calling anonymous function that is just function, it will operate or start as soon as possible when the browser is interpreting your ecma-/javascript

I may be confused, but are you talking about pure javascript? or jquery?
  <script>
    (function(){
 // do something
})();
  </script>

Open in new window

0
More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

 
LVL 13

Expert Comment

by:duncanb7
ID: 40216343
Scott, you are correct and I miss $
And the article mention $(function()) is shorthand for $( document ).ready() at this site. http://learn.jquery.com/using-jquery-core/document-ready/

And my previous post is shown the different between $(document.)ready() and (function()) , javascript self-start function only.

You can test from the following code , if the div tag change color means it work for the code. Finally, it showed only Self-javascript function can not change to green color.

Duncan
<html>
<meta http-equiv="Content-Type" content="text/html; charset=Big5">
<head>
<title>Title</title>
<style>
</style>
<script src="/js/jquery.min.js" type="text/javascript"></script>
<script src="/js/jquery-ui.min.js" type="text/javascript"></script>
<script type="text/javascript">
(function() {
  document.getElementsByClassName('tap')[0].style.color="Green";
});
$(function() {
  document.getElementsByClassName('tap')[1].style.color="Green";
});
$(document).ready(function() {
document.getElementsByClassName('tap')[2].style.color="Green";
});

</script>
<script type="text/javascript">
</script>
</head>
<body  >
<div class="tap">Self-javascript</div>
<div class="tap">$(function())</div>
<div class="tap">Ready-jquery</div>
</body>
</html>

Open in new window

0
 
LVL 13

Expert Comment

by:duncanb7
ID: 40216355
Scott, you agree ?
0
 
LVL 53

Expert Comment

by:Scott Fell, EE MVE
ID: 40216363
Thank you Duncan.   Your description matched something for pure javascript.  If you add the $ or jquery in front, now you are talking about jquery as you suggest, but then the description you gave is incorrect.

The simple correct answer to the question,
is $(document).ready(function() the same as $(function () {
Yes, they are both the same.
0
 
LVL 13

Expert Comment

by:duncanb7
ID: 40216365
Thanks for your reply , please review my post I didn't suggest anything  and I just verify  it by testing for all of you from your previous post to me.

The answer is Yes to the question and  mentioned at this site
http://learn.jquery.com/using-jquery-core/document-ready/

Duncan
0
 
LVL 43

Expert Comment

by:Rob
ID: 40223340
To clarify,

$(document).ready(function() {}); is capturing the "ready" event when the document had loaded.

$(function() {}); is passing an anonymous function to the jQuery class. JQuery is configured such that the anonymous function is only called once the DOM has loaded.

More info on frameworks here:
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_12264-Javascript-Frameworks-what-are-they.html
And JavaScript objects (that is all jQuery is)
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_13138-Javascript-is-just-an-Object.html
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

717 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