0

I want my user to input some text in an input field and click a button "Evaluate" on clicking that button I will convert that inputted text into a JavaScript object if I could and will show all of it's members(properties and functions).

For example if user inputs "document" or "window" I will show all members of document and window respectively.

Iterating through the members of an object is done, but when user inputs some text in input text field and I get that text than that value is treated as String of course how can I convert that text into an object like "document" to document and "window" to window ???

4 Answers 4

3

You can use "eval". Edit: Thanks to the comments of @Victor and @Cerbrus for pointing out that eval is unnecessary here. You can also use window[input].

var input = "document";
var obj = eval(input);
for (var prop in obj) console.log(prop + ": " + obj[prop]);

If it's a member of another object (ie, a global object), then you can use the bracket-notation:

var hello = "world";
var variableName = "hello";
console.log(JSON.stringify(window[variableName]));
Sign up to request clarification or add additional context in comments.

7 Comments

+1 but I would recomend using window[variablename], it's safer in the question context
Note: window["window"] or window["document"] will also work.
If you're only looking at global objects then you could use bracket notation for everything and have a special case for window. That way you're not opening yourself up to the odd behaviour possible with eval.
@Cerbrus incidentally, apparently that's also why JSON.stringify(window) throws a "circular reference" error -- it has a reference to itself.
thanx all, it helped a lot, this is the fiddle " jsfiddle.net/zkHvL " that I have created by this help, plz review and update it if it can be done more nicely.
|
1

The proposed solution won't work for sub-properties (ex. window.document.body). I've written a small jsFiddle that should work for most cases. It certainly lacks real error checks but should be a decent place to start http://jsfiddle.net/a6A4m/1/

 var showProperties = function(text) {
        var output = '';
        var object = window;

        // if it isn't the global window object
        if (text !== 'window') {
            var parts = text.split('.');

            // Since we're using pop, we need to reverse it first.
            parts = parts.reverse();

            while (parts.length > 0) {
                object = object[parts.pop()];
            }
        }

        $('#content').html(getOutput(object));
    };

Comments

0

If you getting input value like that

var selector = $('input').val();

Then the object will be

$(selector)

2 Comments

You should mention that you are using jquery.
There is jquery tag in that post.
0

If your string is JSON format, or the string is a name of javascript object, here a way I saw jQuery.parseJSON do to convert string to object.

var s2o = (new Function("return " +val+";"))();

suppose val contains the value from text input.

It's kind like using Function object as 'compiler', so that you get return a javascript object.

If user pass in document, it give out document object;
if user pass in {id:1, name:"test"}, it give out that object.

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.