Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 16th, 2006, 03:10 AM
Registered User
 
Join Date: Dec 2006
Location: , , Malaysia.
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>
Reply With Quote
  #2 (permalink)  
Old December 16th, 2006, 06:14 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
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)
Reply With Quote
  #3 (permalink)  
Old December 16th, 2006, 08:27 AM
Registered User
 
Join Date: Dec 2006
Location: , , Malaysia.
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

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 06:59 PM.


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