HighDots Forums  

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

Javascript JavaScript language (comp.lang.javascript)


Discuss Using eval('(' + evalText + ')') in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Chuck Anderson
 
Posts: n/a

Default Using eval('(' + evalText + ')') - 06-30-2009 , 09:50 PM






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
*****************************

Reply With Quote
  #2  
Old   
Kiran Makam
 
Posts: n/a

Default Re: Using eval('(' + evalText + ')') - 07-01-2009 , 12:38 AM






Quote:
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

Reply With Quote
  #3  
Old   
Jorge
 
Posts: n/a

Default Re: Using eval('(' + evalText + ')') - 07-01-2009 , 03:50 AM



On Jul 1, 6:38*am, Kiran Makam <kiranm... (AT) gmail (DOT) com> wrote:
Quote:
(...)
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.

Reply With Quote
  #4  
Old   
Chuck Anderson
 
Posts: n/a

Default Re: Using eval('(' + evalText + ')') - 07-01-2009 , 05:42 PM



Kiran Makam wrote:
Quote:
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

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
http://www.cycletourist.com
Turn Off, Tune Out, Drop In
*****************************

Reply With Quote
  #5  
Old   
Peter Michaux
 
Posts: n/a

Default Re: Using eval('(' + evalText + ')') - 07-02-2009 , 12:40 AM



On Jul 1, 2:42*pm, Chuck Anderson <cycletour... (AT) invalid (DOT) invalid>
wrote:
Quote:
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

Reply With Quote
  #6  
Old   
Garrett Smith
 
Posts: n/a

Default Re: Using eval('(' + evalText + ')') - 07-03-2009 , 04:46 PM



Kiran Makam wrote:
Quote:
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/

Reply With Quote
  #7  
Old   
Chuck Anderson
 
Posts: n/a

Default Re: Using eval('(' + evalText + ')') - 07-03-2009 , 06:00 PM



Garrett Smith wrote:
Quote:
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
http://www.cycletourist.com
Turn Off, Tune Out, Drop In
*****************************

Reply With Quote
  #8  
Old   
Peter Michaux
 
Posts: n/a

Default Re: Using eval('(' + evalText + ')') - 07-03-2009 , 11:18 PM



On Jul 3, 1:46*pm, Garrett Smith <dhtmlkitc... (AT) gmail (DOT) com> wrote:
Quote:
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

Reply With Quote
  #9  
Old   
Garrett Smith
 
Posts: n/a

Default Re: Using eval('(' + evalText + ')') - 07-04-2009 , 01:14 AM



Peter Michaux wrote:
Quote:
On Jul 3, 1:46 pm, Garrett Smith <dhtmlkitc... (AT) gmail (DOT) 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/

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.

Quote:
Peter
Garrett

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

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.