tags:

views:

61

answers:

4

When creating functions for re-use, is it possible to create a function inside the:

  $(document).ready(function () {
   });

block of code?

+3  A: 

Yes, however it will only be available inside that scope.

CharlesLeaf
That depends on whether or not the new function is added with a var statement (local scope), without var (global scope--bad), or attached as a method to an existing object (like a jQuery plugin).
AutoSponge
You're right, good thing the more detailed answer has already been upvoted more ;) I assumed he was talking about normal/local functions due to a lacking example, so didn't think about the many other ways.
CharlesLeaf
+11  A: 

Yes, but they can't be referenced outside of there, for example:

$(document).ready(function () {
  function myFunc() { }
  $(".class").click(myFunc);
  //or myFunc();
});

Is valid, where as this wouldn't be:

$(document).ready(function () {
  function myFunc() { }
});
myFunc();

Or the more common inline issue, where it can't access the function as a result of scoping:

<button onclick="myFunc()">Something</button>
Nick Craver
@downvoter - care to comment?
Nick Craver
Nick - You mean you actually expect someone to be able to explain their down-vote? You must be new here. ;o) I suppose you also expect people to *not* up-vote *incorrect* answers. Picky, picky.
patrick dw
+1 Good answer. I think it is generally polite to explain downvotes. It is also helpful to others to see the reasons for them. It also gives the answerer a chance for rebuttal and to have the downvote removed following a good hearted argument.
Daniel Dyson
There's a time and place for everything. http://stackoverflow.com/questions/2645344/functions-inside-or-outside-jquery-document-ready/2645353#2645353
@user257493 - I agree, and if you're *only* using (and reusing) it within this scope...I'd say there's absolutely nothing wrong with sticking it inside.
Nick Craver
+1 for the explanation of why scoping can influence inline Javascript.
Peter Ajtai
A: 

the syntax

xy = function() {
}

allways creates a globaly accessable function (if the variable was not initialized before). But you should not create functions inside $(document).ready(); as there is no need to wait for the DOM to load the function. Into $(document).ready(); you put code you want to execute if the complete HTML became loaded by the browser.

Maybe you mean the "own scope" syntax:

(function($) {
  /* code */
})(jQuery);

But also there: If you want to have global functions, why put them into a local scope? :-)

sod
Global isn't the same as reusable, I think you're mixing the two up...the OP may be using the function 10 times, but only when binding things in `document.ready` for example.
Nick Craver
A: 

Yes. There is even a way to use it outside of that block:

var gs = {}
gs.func = function() {} // A dummy to avoid errors

$(document).ready(function () {
    gs.func = function() {...} // redefinition when the document is ready
});

This way, you can use gs.func() everywhere, it just won't do anything until the page has loaded.

Aaron Digulla
Not anywhere... only in the part of the script that comes [ **after** ](http://jsfiddle.net/fKW27/) `gs.func = ...`. This can be outside doc ready as you say. This contrasts with using using `function name() {...}` within doc ready... which can then be used anywhere **within** doc ready... even [ **before** ](http://jsfiddle.net/vZnBV/) the line `function name() {...}`.
Peter Ajtai
You missed the point. The function can be used without an exception anywhere in the page, *it just won't do anything*.
Aaron Digulla