3

I have a 2000 line jquery file, I just broke up the file into smaller ones, If I have a function in the first file, that file # 2 is referring to, it's coming up undefined.

Every file is is wrapped in a jquery ready function, What's the best way to do this?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
matthewb
  • 3,462
  • 8
  • 37
  • 53

7 Answers7

6

If the function in question is declared within the scope of the ready handler, it won't be accessible to any other code, including other ready handlers.

What you need to do is define the function in the global scope:

function foo()
{
    alert('foo');
}

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

P.S. A more concise way of adding a ready handler is this:

$(function()
{
   foo();
});

Edit: If the contents of each of your divided ready handlers rely on the previous sections, then you can't split them up, for the reasons outlines above. What would be more sensible would be to factor out the bulk of the logic into independent functions, put these in their own files outside the ready event handler, and then call them from within the handler.

Edit: To further clarify, consider this handler:

$(function()
{
    var foo = 'foo';
    var bar = 'bar';

    alert(foo);
    alert(bar);
});

I might then split this up:

$(function()
{
    var foo = 'foo';
    var bar = 'bar';
});

$(function()
{
    alert(foo);
    alert(bar);
});

The problem with this is that foo and bar are defined in the first handler, and when they are used in the second handler, they have gone out of scope.

Any continuous flow of logic like this needs to be in the same scope (in this case, the event handler).

Will Vousden
  • 32,488
  • 9
  • 84
  • 95
2

Function definition should not be wrapped in another function. Not unless you really want that function definition to be private. And if I understand correctly that's not your intention.

Only wrap function invocation in the jQuery ready function.

If you're worried about your functions clashing with third party function names then namespace them:

var myFunctions = {}
myFunctions.doThis = function () {}
myFunctions.doThat = function () {}

But really, you only need to worry about this if you're creating a mashup or library for others to use. On your own site YOU have control of what gets included in javascript.

slebetman
  • 109,858
  • 19
  • 140
  • 171
1

Actually, for performance reasons, it may be better to keep it in one file; multiple requests actually can take up more bandwidth... but as separate files, you would need to order them in a particular order so that there is a logical sequence. Instead of having everything in a document.ready, have each script define a method, that the page will execute within its own document.ready handler, so that you can maintain that order.

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
  • I am not exactly sure if I agree with you about putting all in one file to save bandwidth. From my experience (which is limit when it some to JavaScript), separating a file actually save bandwidth as the separated files can be cached (as they are not loaded every time). This is even more useful when the file is use by many pages. Just a thought. – NawaMan Jan 04 '10 at 20:48
  • A lot of talk on the web mentions combining scripts for performance reasons, because of the way that multiple script files get downloaded to the client; there's some overhead which increases the total time/size of the files, as if they were one. Plus, script minifying (like YUI or MS AJAX's components) help shrink files too. – Brian Mains Jan 04 '10 at 21:26
1

Most likely the reason it's coming up undefined is because when you have separate ready calls, the scope of the code inside those calls is different.

I would reorganize my code. Any shared functions can be attached to the jQuery object directly, using $.extend. This is what we do for our application and it works well.

See this question. Hope it helps.

Community
  • 1
  • 1
steve_c
  • 6,235
  • 4
  • 32
  • 42
0

Everyfile shouldnt have a ready function. Only one file should have the ready function and that should be the last file.

Zaje
  • 2,281
  • 5
  • 27
  • 39
0

Functions provide scope in JavaScript. Your code in the jquery.ready is an anonymous function, so it is unaware of the other scopes. remove the wrappings for those JavaScript functions and declare them as regular functions, a la

$(document).ready(function ()
{
   functionFromFile1();
   functionFromFile2();
};
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Tim Hoolihan
  • 12,316
  • 3
  • 41
  • 54
0

"wrapped in a jquery ready function" is nothing else than binding stuff to the ready event that is fired when jQuery thinks the DOM is ready.

You should only bind methods that is depending on the DOM to the ready event. It doesnt matter how many binds you make, all of the methods will be executed in the binding order in the end.

David Hellsing
  • 106,495
  • 44
  • 176
  • 212