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.
jQuery Document Ready Example $(document). ready(function() { //DOM manipulation code }); You call jQuery's $ function, passing to it the document object. The $ function returns an enhanced version of the document object.
The ready() method is used to make a function available after the document is loaded. Whatever code you write inside the $(document ). ready() method will run once the page DOM is ready to execute JavaScript code.
jQuery ready() Method 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.
ready() method offers a way to run JavaScript code as soon as the page's Document Object Model (DOM) becomes safe to manipulate. This will often be a good time to perform tasks that are needed before the user views or interacts with the page, for example to add event handlers and initialize plugins.
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.
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.
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