Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using eval('(' + evalText + ')')

3 views
Skip to first unread message

Chuck Anderson

unread,
Jun 30, 2009, 9:50:16 PM6/30/09
to
This seems like a very basic question - but I have searched Google and
the javascript faq and I cannot find an explanation.

What is the purpose of using eval in this manner (and this would be my
usage - data returned from an XMLHttpRequest).

var data = eval('(' + req.responseText + ')');

Why the inner parenthesis?

TIA

--
*****************************
Chuck Anderson • Boulder, CO
http://www.cycletourist.com
Turn Off, Tune Out, Drop In
*****************************

Kiran Makam

unread,
Jul 1, 2009, 12:38:07 AM7/1/09
to

>
> var data = eval('(' + req.responseText + ')');
>

In JavaScript, "{" and "}" are used to denote
- a block of code and
- an object literal

when you execute eval( "{'key':value}" ), "{" is executed as a block
of code instead of an object literal. Hence inner parenthesis is used
to force eval to treat the json-string as an object literal.

You can go through this for detailed explanation:
http://rayfd.wordpress.com/2007/03/28/why-wont-eval-eval-my-json-or-json-object-object-literal/

Kiran Makam

Jorge

unread,
Jul 1, 2009, 3:50:20 AM7/1/09
to
On Jul 1, 6:38 am, Kiran Makam <kiranm...@gmail.com> wrote:
> (...)
> You can go through this for detailed explanation:http://rayfd.wordpress.com/2007/03/28/why-wont-eval-eval-my-json-or-j...

<quote>
Note something about this parser though: it is REALLY strict. It won’t
parse perfectly OK object literals like { name: “Ray”, age: 31 }. Eval
parses this without problems:
</quote>

It's a common error to build JSON texts without the required enclosing
quotes in the object's properties' names, but such not-really-JSON
texts won't pass the eval() when/if any of the properties' names are
=== to a JS reserved word :

eval('( { do: 5, while: 10 } )'); -> err
eval('( { "do": 5, "while": 10 } )'); -> ok

or when/if the properties' names just somehow look awful to the JS
parser:

eval('( { : 5, *: 6, how many: 7 } )'); -> err
eval('( { "": 5, "*":6, "how many": 7 } )'); -> ok


IMO, this things ought to be in the FAQ:

http://www.google.com/search?&q=JSON+site:jibbering.com/faq/

"Your search - JSON site:jibbering.com/faq/ - did not match any
documents."

: pitiful.

--
Jorge.

Chuck Anderson

unread,
Jul 1, 2009, 5:42:03 PM7/1/09
to

Ahhhhh ... syntactic ambiguity. Why didn't you just say so? ��Ԭ

No wonder I couldn't figure it out for myself.

So .... while I'm on this topic. I'm sending data back to an
XMLHttpRequest from Php and not even using JSON. I merely use:

<?php
echo "var somevar1 = 'somevalue1';";
echo "var somevar2 = 'somevalue2';";
exit;
?>

Then I just "eval(req.responseText);"

Is there any need for me to convert this to an object and use JSON
instead when simply passing back a group of simple variables?

<?php
$json = array()
$json['somevar1'] = 'somevalue1';
$json['somevar2'] = 'somevalue2';
$json_encoded = json_encode($json);
echo $json;
exit;
?>

--
*****************************
Chuck Anderson � Boulder, CO

Peter Michaux

unread,
Jul 2, 2009, 12:40:58 AM7/2/09
to
On Jul 1, 2:42 pm, Chuck Anderson <cycletour...@invalid.invalid>
wrote:

> Kiran Makam wrote:
>
> >> var data = eval('(' + req.responseText + ')');
>
> > In JavaScript, "{" and "}" are used to denote
> > - a block of code and
> > - an object literal
>
> > when you execute eval( "{'key':value}" ), "{" is executed as a block
> > of code instead of an object literal. Hence inner parenthesis is used
> > to force eval to treat the json-string as an object literal.
>
> > You can go through this for detailed explanation:
> >http://rayfd.wordpress.com/2007/03/28/why-wont-eval-eval-my-json-or-j...
>
> > Kiran Makam
>
> Ahhhhh ... syntactic ambiguity. Why didn't you just say so? ô¿Ô¬

>
> No wonder I couldn't figure it out for myself.
>
> So .... while I'm on this topic. I'm sending data back to an
> XMLHttpRequest from Php and not even using JSON. I merely use:
>
> <?php
> echo "var somevar1 = 'somevalue1';";
> echo "var somevar2 = 'somevalue2';";
> exit;
> ?>
>
> Then I just "eval(req.responseText);"
>
> Is there any need for me to convert this to an object and use JSON
> instead when simply passing back a group of simple variables?

Yes there is a good reason to use JSON rather than arbitrary
JavaScript: non-browser clients. JSON is a data interchange format and
libraries to read and write JSON exist in many languages. If a non-
browser client is consuming your data in JSON format then the client
only needs a JSON library. If your server sends back arbitrary
JavaScript then either ad-hoc parsing or a full JavaScript parser
would be necessary and either of these is an ugly solution.
Transporting data in a data format, like JSON, for flexibility is a
good idea.

Peter

Garrett Smith

unread,
Jul 3, 2009, 4:46:37 PM7/3/09
to
Kiran Makam wrote:
>
>> var data = eval('(' + req.responseText + ')');
>>
>
> In JavaScript, "{" and "}" are used to denote
> - a block of code and
> - an object literal
>
> when you execute eval( "{'key':value}" ), "{" is executed as a block
> of code instead of an object literal. Hence inner parenthesis is used
> to force eval to treat the json-string as an object literal.
>

That ought to be in the FAQ under:
http://jibbering.com/faq/#eval

with a link to http://json.org/

Garrett
--
comp.lang.javascript FAQ: http://jibbering.com/faq/

Chuck Anderson

unread,
Jul 3, 2009, 6:00:16 PM7/3/09
to
Garrett Smith wrote:
> Kiran Makam wrote:
>
>>> var data = eval('(' + req.responseText + ')');
>>>
>>>
>> In JavaScript, "{" and "}" are used to denote
>> - a block of code and
>> - an object literal
>>
>> when you execute eval( "{'key':value}" ), "{" is executed as a block
>> of code instead of an object literal. Hence inner parenthesis is used
>> to force eval to treat the json-string as an object literal.
>>
>>
>
> That ought to be in the FAQ under:
> http://jibbering.com/faq/#eval
>
> with a link to http://json.org/
>
> Garrett
>

That's where I first went to look for my answer.

--
*****************************
Chuck Anderson � Boulder, CO

Peter Michaux

unread,
Jul 3, 2009, 11:18:33 PM7/3/09
to
On Jul 3, 1:46 pm, Garrett Smith <dhtmlkitc...@gmail.com> wrote:
> Kiran Makam wrote:
>
> >> var data = eval('(' + req.responseText + ')');
>
> > In JavaScript, "{" and "}" are used to denote
> > - a block of code and
> > - an object literal
>
> > when you execute eval( "{'key':value}" ), "{" is executed as a block
> > of code instead of an object literal. Hence inner parenthesis is used
> > to force eval to treat the json-string as an object literal.
>
> That ought to be in the FAQ under:http://jibbering.com/faq/#eval
>
> with a link tohttp://json.org/

<FAQENTRY>

Peter

Garrett Smith

unread,
Jul 4, 2009, 1:14:04 AM7/4/09
to
> <FAQ**TRY>
>

Yes, duly noted! I actually already added that in the draft on my local
machine.

BTW - Tagging FAQ**TRY can help, but I don't search "<FAQENTRY>" that
often.

> Peter

0 new messages