Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console access to Javascript variables local to the $(document).ready function

Tags:

How can I access some variables inside

$(document).ready(function(){     var foo=0;     var bar = 3; }); 

from Google chrome console? If I try alert(foo), I will get a message that it is not defined.

like image 827
Mircea Voivod Avatar asked Dec 07 '12 15:12

Mircea Voivod


People also ask

What does $( document .ready function () do?

$( document ). ready()A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you. Code included inside $( document ). ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute.

How do I view JavaScript variables in Chrome?

To view any variable in chrome, go to "Sources", and then "Watch" and add it. If you add the "window" variable here then you can expand it and explore.

What is the purpose of the following code $( document .ready function (){ $( Div first (); });?

$(document). ready() indicates that code in it needs to be executed once the DOM got loaded. It won't wait for the images to load for executing the jQuery script.

What is use of ready function in JavaScript?

The ready event occurs when the DOM (document object model) has been loaded. Because this event occurs after the document is ready, it is a good place to have all other jQuery events and functions. Like in the example above. The ready() method specifies what happens when a ready event occurs.


2 Answers

Put a breakpoint with the debugger. You'll get full access to them when the debugger will stop.

Other answers telling you to put them in the global scope are bad. Don't use bad practices just because you don't know how to use the right tools.

like image 175
Florian Margaine Avatar answered Dec 16 '22 02:12

Florian Margaine


You can't access these variables because they are defined within a functional closure. The only way you could do it is if you made a global reference to them outside your function's scope.

var foo, bar;  $(document).ready(function(){     foo = 0;     bar = 3; }); 
like image 36
Xophmeister Avatar answered Dec 16 '22 03:12

Xophmeister