curly_brackets
curly_brackets

Reputation: 5598

Calling function inside jQuery document ready

While debugging, I always use Firebug and try to call functions and show variables. However I can't when the function or variable is defined within $(document).ready.

How can I access these variables? Can I type something like a namespace, like document.ready.variableName or how can I see this?

Thank you in advance.

Upvotes: 14

Views: 24501

Answers (7)

James Jithin
James Jithin

Reputation: 10565

Declare the variable in global scope:

E.g.

<script type="text/javascript">
var globalVar = null;
$(document).ready(
function() {
     globalVar = "This is the value";
}
);

function TestFunc() {
    alert(globalVar);
}
</script>

Here, if you call the TestFunc() anytime after the page load, you will see the value assigned in the ready() function.

Upvotes: 1

Mr_Nizzle
Mr_Nizzle

Reputation: 6714

If you have

var x = "foo"
$(document).ready(function(){
  alert(x); // foo
});

You can see the x variable anywhere, but if you you declare a variable y into the document ready It'll be only accessible within the document ready:

var x = "foo"
$(document).ready(function(){
  alert(x); // foo
  var y = "bar"
  alert(y); // bar
});
alert(y); // undefined

Upvotes: 0

millimoose
millimoose

Reputation: 39950

Global variables and functions can be created by assigning them as a property of window:

$(function(){
    window.foo = function foo() {
        // …
    }
});

foo() should be accessible anywhere after that handler is executed.

Upvotes: 19

topek
topek

Reputation: 18979

That's what debugging is for. In all major browsers (including IE), you can set breakpoints in the javascript code. When this is done the script halts and you can inspect your variables.

Here some links:

Upvotes: 2

James Johnson
James Johnson

Reputation: 46047

Not sure I fully understand the issue, but couldn't you just declare the variables outside of document ready?

var a = "bar";

$(document).ready(function(){
    a = "foo";
});

If you're using firebug, you should be able to call console.log within document ready, which might give you what you're looking for.

Upvotes: 0

arb
arb

Reputation: 7863

It depends on how you declare the variables inside the .ready() function. If you do var x = "test", then no, they are only accessible inside the scope of the ready function. If you do something like x="test", then that is available in the global scope and you can just access it like alert(x); or alert(window.x);

You probably don't want to define variables inside the ready function though if you are trying to use them outside the ready function.

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

How can I access these variables?

Well, you can't. Everything that you define inside an anonymous function such as what you are using in the $(document).ready is scoped to this anonymous function. It's private and not accessible to the outside.

So you could put your console.log inside the $(document).ready if you needed to inspect some private variable that is defined in its scope.

Upvotes: 2

Related Questions