tags:

views:

322

answers:

6

Hello!

Is there a way to find JavaScript variable on the page (get it as an object) by its name? Variable name is available as a string constant.

A: 

You could use eval()

James L
never *ever* use eval where you don't have to
annakata
vote up. simple question deserves a simple answer. i'd go with eval() if I for whatever reason needed to look at the contents of a variable by its name.
Peter Perháč
A). what's not simple about window[foo]? B). speed and security don't concern you then? eval is toxic, seriously
annakata
I second "annakata".
Steve Harrison
I third annakata.
bobince
@annakata - i see your point, and never meant to say this solution is better than yours. i'm just saying that eval -does- resolve the question asked.
Peter Perháč
I'd hate for anyone to think this was a competition - I really honestly don't downvote for that - and I accept that eval does technically solve the problem, but nonetheless eval is the nuclear option. It's better if you don't have to use it.
annakata
A: 

Javascript variables aren't "on the page", like DOM elements. You can access them only if they are available from your scope.

What are you trying to do?

Jaka Jančar
Actually in most respects JS variables very much are available as if they were in the DOM
annakata
You can access them from outside of your scope? How can you find the parent object of an object, without knowing it in advance? You can't (there can be multiple). In which way are they similar to DOM elements?
Jaka Jančar
Bear in mind that JS is *mostly* what you'd call public scoped, that the window object is the root for JS objects, and that the OP already has the "path" to the variable.
annakata
I understood that he only had a name (e.g. he knew that a variable named "my_foo_bar_index" was being used *somewhere*). Well, ok :)
Jaka Jančar
Yeah, he'd be screwed if that were the case :)
annakata
+4  A: 
<script>
var a ="test";
alert(a);
alert(window["a"]);
alert(eval("a"));
</script>
Catalin DICU
+7  A: 

All JS objects (which variables are) are available within their scope as named properties of their parent object. Where no explicit parent exists, it is implicitly the window object.

i.e.:

var x = 'abc';
alert(window['x']); //displays 'abc'

and for a complex object:

var x = {y:'abc'};
alert(x['y']); //displays 'abc'

and this can be chained:

var x = {y:'abc'};
alert(window['x']['y']); //displays 'abc'
annakata
that's only true for globally scoped variables - if the scope is function-level, there's no object which allows access to the lexical environment
Christoph
Sure, but nothing helps you there. This assumes you can express the var as a dot notation construct.
annakata
A: 

If your string references a 'deep' property of a global, like 'Yankee.console.format' you can step through the references:

String.prototype.deref= function(){
    // remove leading and trailing quotes and spaces
    var obj= this.replace(/(^[' "]+|[" ']+$)/g,'');

    var M= obj.match(/(^[\w\$]+(\.[\w\$]+)*)/);
    if(M){
     M= M[1].split('.');
     obj= window[M.shift()];
     while(obj && M.length) obj= obj[M.shift()];
    } 
    return obj || this;
}
kennebec
A: 

If you are wanting a variable that is declared in the global context, it is attached to the window object. ex: window["variableName"]. All variables are a hash table value within their scope.

If you have to use dotted notation, then you will want to follow kennebec's suggestion, to navigate through the object hierarchy. eval() can work as well, but is a more expensive operation than is probably needed.

Tracker1