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.
$( 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.
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.
$(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.
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.
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.
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; });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With