Eval a variable in javascript

Hi all

I didn’t think this would be a problem at all, but basically, I need to pass a string into javascript, and then be able to use the variables that are in that string. Like so


var string = '{heading:"Im looking for"};';
var values = eval(string);
alert(values.heading);

This results in “undefined”, bleek!

Does anyone know what i’m doing wrong here…
Thanks in advance
Bye

The JSON library from http://www.json.org/js.html is the best way to do that.

Check it out with below code.

It may work for you.

var string = ‘var heading=“Im looking for”’;
eval(string);
alert(headling);

Hey awesome!
Thanks for that site felgall
They had an eval example right there.

The corrected code looks like this


var string = '{heading:"Im looking for"}';
var values = eval('(' + string + ')');
alert(values.heading);

I removed the ; from the string variable, and changed the eval argument.

Thanks for your suggestion archanapatel but i’m going to stick with the json notation :wink:

Cheers all