Get all Javascript Variables?

Is there a way for javascript to detect all assigned variables? For example, if one js file creates a bunch of vars (globally scoped), can a subsequent file get all the vars without knowing what they're named and which might exist?

Thanks in advance :)

EDIT, Question Part 2:

How do I get the values of these variables? Here is what I have attempted:

This is what I ended up with, as per comment suggestions:

for (var name in this) {
    variables[name] = name;
    variables[name]=this[name]
}

Answers:

Answer

Flanagan's "JavaScript - The Definitive Guide" gives the following on page 653:

var variables = ""
for (var name in this)
    variables += name + "\n";
Answer

For Firefox, you can see the DOM tab -- easy, though not an answer to your question.

The for in loop provided in Kinopiko's answer will work, but not in IE. More is explained in the article linked below.

For IE, use the RuntimeObject.

if(this.RuntimeObject){
    void function() {
        var ro = RuntimeObject(),
            results = [],
            prop;
        for(prop in ro) {
            results.push(prop);
        }
        alert("leaked:\n" + results.join("\n"));
    }();
}

See also:

Answer

There is the this variable. This is an object or an array, and you can simply put:

for(i in this) { //do something }

Unfortunately, it will return everything under the this object.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.