Wrox Programmer Forums
|
BOOK: Professional Ajax ISBN: 978-0-471-77778-6
This is the forum to discuss the Wrox book Professional Ajax by Nicholas C. Zakas, Jeremy McPeak, Joe Fawcett; ISBN: 9780471777786
Important: For the new 2nd edition of this book, please post here instead: [url="http://p2p.wrox.com/forum.asp?FORUM_ID=307"]http://p2p.wrox.com/forum.asp?FORUM_ID=307[/url]
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional Ajax ISBN: 978-0-471-77778-6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 16th, 2006, 03:10 AM
Registered User
 
Join Date: Dec 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to keenlearner
Default page 194, eval() problem

I am wodering why my code does not work when using eval, but it works without using eval() method. Can i know where am i suppose to use the eval() method and when ?

This is my code that does NOT alert :

Code:
<script type="text/javascript">
var b = {
    "c" : ["red", "white", "blue"],
    "d" : [2,4]
}

var e = eval("("+ b +")");
alert(e.c[0]);
</script>
This is my code that alert :
Code:
<script type="text/javascript">
var b = {
    "c" : ["red", "white", "blue"],
    "d" : [2,4]
}

alert(b.c[0]);
</script>
 
Old December 16th, 2006, 06:14 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You only need eval when you are trying to turn a string, normally received from a web service, into interpreted JavaScript. In the example above you already have code. Try:
Code:
var s = "{c: ["red\", \"white\", \"blue\"], d: [2, 4]}";
var e = eval("(" + s + ")");
alert(e.c[0]);
--

Joe (Microsoft MVP - XML)
 
Old December 16th, 2006, 08:27 AM
Registered User
 
Join Date: Dec 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to keenlearner
Default

Thank's joe, your code works and also my code after i notice that you remove the double quote strings on key name and escape the double quotes on the values. Now i get it. Thank's again






Similar Threads
Thread Thread Starter Forum Replies Last Post
problem installing RS eval edition geoffc Reporting Services 0 November 8th, 2006 10:11 PM
problem installing RS eval edition geoffc BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 0 November 8th, 2006 06:52 PM
CH7: RegExp Object PG 194 and 195 xZanU BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 5 January 12th, 2006 04:31 PM
Eval??? tsimsha Classic ASP Basics 2 January 17th, 2005 10:02 PM
Multiple Constructors Page 194 NUTSHELL BOOK: Beginning Java 2 2 February 5th, 2004 11:00 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.