Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 676
curly_brackets Avatar asked Nov 15 '11 19:11

curly_brackets


People also ask

How do you call a function in document ready jQuery?

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.

Can we write function inside document ready?

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.

Why jQuery methods are inside a document ready event?

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.

What does the jQuery ready () function do?

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.


2 Answers

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.

like image 174
millimoose Avatar answered Oct 02 '22 10:10

millimoose


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.

like image 35
Darin Dimitrov Avatar answered Oct 02 '22 09:10

Darin Dimitrov