26

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.

5
  • 1
    define them outside of $(document).ready( and then access them inside as well... Commented Dec 7, 2012 at 15:48
  • Are you sure jQuery is being loaded? Commented Dec 7, 2012 at 15:49
  • I don't want to declare them outside for security reasons, I'm just curious how or if they can be accessed with the console :) Commented Dec 7, 2012 at 15:49
  • 2
    @Vodaldrien: They can't. Commented Dec 7, 2012 at 15:50
  • 1
    Looks like a duplicate of stackoverflow.com/questions/11192875/… Commented Dec 7, 2012 at 15:51

8 Answers 8

51

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Could you link some information about this or what debugger should I use? Would be really appreciated. I'm new with javascript/jquery and I'm trying to check if those variables can be accessed in any way for security reasons. Thanks
@Vodaldrien Use Firebug or Chrome Dev Tools. These variables can be accessed by whoever uses your code. In any case. Don't think you can secure your code this way. Once you give a webpage to a client, he can do anything he wants with it.
@Vodaldrien Hiding the variables is more about avoiding bugs and having a cleaner codebase :)
@RocketHazmat for Chrome Dev tools, look here. For Firebug, look into its doc.
this is the correct answer! just type in debugger anywhere in your js. It can be anywhere, including definitions, not just procedural calls..
|
20

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;
});

7 Comments

6 upvotes for an answer advising to use global variables? Really?
I'm not advising using globals, just that it's the only way to do this!
No, it's not. See my answer.
I think it's more important for the OP to understand closures than how to use debugging tools.
At the risk of starting a flame war: "The only way you could do it..." doesn't imply advice, just a solution. The only solution within the context of the language is to use an external scope (e.g., global scope, or some other namespace). However, as you say, this restriction can be lifted using sophisticated debugging tools; but this is outside the specification of the language, itself.
|
17

Why not do a proper expose variable ?

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

    $.exposed = {
        foo: foo,
        bar: bar
    }
});

Check your variables by doing

console.log($.exposed.bar)

2 Comments

Super useful for debugging! Nice.
This is a great idea and more cross browser suitable, although will have to be strict and not forget to remove them from here or use it as a shortcut, and then it becomes yet another window.variable problem. Otherwise, using debugger; is probably the best as it pretty much forces you to remove it before deployment.
5

You can't since the are in a closure space. Here it explains how closure works (How do JavaScript closures work? ). To access the varible just set a breakpoint inside the $(document).ready function

Comments

4

If you really need to access these variables from different parts of your code (initialize them on document ready, then accessing them elsewhere, for example), then you have to declare them outside the function closure.

If and only if this is the case, I'm not a fan of cluttering the global space. I would suggest you to use a base object for that :

var myObj = {};

$(function() {
   myObj.foo = 0;
   myObj.bar = 3;
});

Note that they will only be set once the document is loaded! Therefore alert(myObj.foo); (or something similar) place immediately after the $(function() { ... }); block will yield undefined!

If you only need to access them inside that context, then do not declare anything outside the function. And try to debug your code with other methods. With chrome, console.log is quite helpful, for instance.

5 Comments

The amount of variables in is cut in half. It is definitely better than declaring two variables.
If the the variable foo needs to be accessed elsewhere, then you have no choice but to declare it outside, in a shared object. This is not bad practice, it is used all the time in JavaScript code. Some people abuse of it, but what I'm suggesting does not.
But it does not need to declared elsewhere.... did you even read the OPs question?
"Vote to remove this post" ? ... wow... I have read the question, and it is not specified. You speculate as much as I do, Neal.
Not sure why this answer got voted down. It basically says the same thing as the accepted answer, but adds only a single global object instead of a bunch of random global variables. This is definitely the best of the "make it global" answers.
2
$(document).ready(function(){
    window.foo=0;
    window.bar = 3;
});

You expose those vars into global scope(really not advised)

Comments

1

define them outside of $(document).ready() and then access them inside as well.

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

Comments

0

Actually there is a hackish way around it. You should probably not use it. Set a Breakpoint. That being said, here's the code:

$(document).ready(
    readyClosure = function(access){
        var x = 5; 
        return eval(access);
    }
);
readyClosure('x'); // returns 5

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.