Mohammed
Mohammed

Reputation: 1

Difference between jQuery Ready and jQuery Ready shorthand with parameter ?

While going through old codebase I found a place where the previous software developer is using

$(function(a) {}(A || (A = {})));

which is executing even though the page is not ready yet. It started working after I removed the global variable being passed. Was the code done incorrectly the first time?

final solution:

$(function() {});

Upvotes: 0

Views: 63

Answers (2)

Christoph
Christoph

Reputation: 1630

Yes, it was done incorrectly.

Because of the parenthesis (A || (A = {})) the function given to jQuery was executed immediately. So jQuery doesn't even get a function to call when the page was loaded but rather that function's return value.

The way you corrected it you are giving jQuery a function and you are letting jQuery execute it when the page is ready.

Upvotes: 1

bcr
bcr

Reputation: 3811

Basically what's happening in the first version is you're executing the function and passing it's return value into the on ready shorthand.

It really depends on what's happening in the function body to tell you whether or not it's "correct".

Upvotes: 1

Related Questions