-1

Say I add to First.js:

$(document).ready(

function () {
    dosomething A
    });

function () {
    dosomething C
    });
});

and to Second.js:

$(document).ready(

function () {
    dosomething B
    });
});

will all 3 functions be executed after DOM is ready?

What will be the case when I register

to First.js:

$(document).ready(
A = function () {
    dosomething A
    });

C = function () {
    dosomething C
    });
});

to Second.js:

$(document).ready(
A = function () {
    dosomething A
    });

});

The later will override the first?

TIA

BNL
  • 7,085
  • 4
  • 27
  • 32
Elad Benda
  • 35,076
  • 87
  • 265
  • 471
  • 2
    You have a bunch of syntax errors in your example code; please review and correct it. – Shad Dec 18 '11 at 22:03
  • e.g. `.ready` takes a __function__ as its parameter. – Shad Dec 18 '11 at 22:06
  • I assume this is pseudo-code. I think the question is quite clear, even if there are "syntax errors" in the examples provided. – David Hellsing Dec 18 '11 at 22:07
  • Interpretation of the syntax errors certainly makes a difference here. – BNL Dec 18 '11 at 22:09
  • possible duplicate of [JQuery - multiple $(document).ready ...?](http://stackoverflow.com/questions/5263385/jquery-multiple-document-ready) – Felix Kling Dec 18 '11 at 22:09
  • 2
    @David I disagree; reading the code, I have to make presumptive leaps, and that is inappropriate when "what is the result" is the question. – Shad Dec 18 '11 at 22:11
  • 2
    No one can answer your question unless you clarify *what* you think is overridden and you fix the syntax errors. – Felix Kling Dec 18 '11 at 22:13

4 Answers4

2

"will all 3 functions be executed after DOM is ready?"

Yes. Each time you bind something to be executed at DomReady, jQuery will queue the function in an internal array, then execute them in the same order as they where "inserted".

The later will override the first?

Yes it will, unless you put var before the definition. JavaScript will put A in the window scope, so the next definition will override the first.

function() {
    var A = 0; // this will only exist within the function
}

function() {
    A = 1; // this will be added to the "global" scope (window).
}
David Hellsing
  • 106,495
  • 44
  • 176
  • 212
2

Your first example is invalid syntax. It will cause the javascript interpreter to throw an exception. You need to pass one and only one function to $(document).ready(fn). You can include multiple function calls inside the one function, but you can only pass one function to .ready().

Your second example is also a syntax error - an extra });. If that is removed, it will work and execute that one function.

Your third example in both first.js and second.js is also a syntax error. You can't put arbitrary javascript as the parameter to .ready(). It must be one function reference with proper syntax.

Now, what you may have been trying to ask if you actually provided legal syntax in your examples is that all functions you pass to .ready(fn) will be executed when the document is ready. jQuery keeps an array of all functions that have been passed and executes them all when the document becomes ready. The jQuery documentation for .ready() does not specify the calling order if .ready() has been called multiple times with multiple functions, though one could examine the source code and see what the order is likely to be.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
1

The later will override the first?

No, you can assign multiple functions to individual events.

I'm assuming your syntax errors were unintentional. The way they are written, NO code is executed.

BNL
  • 7,085
  • 4
  • 27
  • 32
  • I up-voted on the assumption that the 'no' was to the question: "The later will override the first?". – David Thomas Dec 18 '11 at 22:03
  • -1 That's not correct. The later code *will* replace the value set by the first code. – Guffa Dec 18 '11 at 22:10
  • That isn't even valid js, so no it won't. You are making an assumption about the code that isn't clear from the question. – BNL Dec 18 '11 at 22:11
-1

Yes, you can bind the same event several times without problems.

Yes, the second code will replace the value set by the first code.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 1
    Why the downvote? If you don't explain what you think is wrong, it can't improve the answer. – Guffa Dec 18 '11 at 23:29