Advertisement
richarduie

evalDemo.html

Nov 6th, 2013
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 3.29 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5.     <title>eval() demo</title>
  6.  
  7.     <script type="text/javascript">
  8.         // utility for my convenience
  9.         function get( eid ) {return document.getElementById( eid ); };
  10.  
  11.         // let's start with the rawest json expression of the object
  12.         // ...rawest json declaration without need to eval()
  13.         // declares an object with a key named employees which has a
  14.         // value that is an array containing three elements, each of
  15.         // which is addressable by numeric (integer) index position and
  16.         // each of which is an object with keys named firstName and
  17.         // lastName
  18.         var rawest = {
  19.             employees:[
  20.                 {firstName:"John",lastName:"Doe" },
  21.                 {firstName:"Anna",lastName:"Smith" },
  22.                 {firstName:"Peter",lastName:"Jones" }
  23.             ]
  24.         };
  25.  
  26.         // now, let's convert the rawest form into an eval()uable string
  27.         var txt2 = '{employees:[' +
  28.             '{firstName:"John",lastName:"Doe" },' +
  29.             '{firstName:"Anna",lastName:"Smith" },' +
  30.             '{firstName:"Peter",lastName:"Jones" }]}';
  31.  
  32.         // let's add a raw json expression of the object with quoted key
  33.         // names (this will operate the same as the original rawest form)
  34.         // ...raw json declaration without need to eval()
  35.         var raw = {
  36.             "employees":[
  37.                 {"firstName":"John","lastName":"Doe" },
  38.                 {"firstName":"Anna","lastName":"Smith" },
  39.                 {"firstName":"Peter","lastName":"Jones" }
  40.             ]
  41.         };
  42.  
  43.         // now, let's convert the raw form into an eval()uable string
  44.         // original question code starts here - - - -
  45.         var txt = '{"employees":[' +
  46.             '{"firstName":"John","lastName":"Doe" },' +
  47.             '{"firstName":"Anna","lastName":"Smith" },' +
  48.             '{"firstName":"Peter","lastName":"Jones" }]}';
  49.  
  50.         // placing txt in "(" and ")" prevents parser from being confused by
  51.         // opening "{" - language spec forbids starting an eval() argument
  52.         // with a curly brace (possible to misinterpret it as a block)
  53.         var obj = eval ("(" + txt + ")");
  54.         // original question code ends here - - - -
  55.  
  56.         // now that you know why the "(" and ")" are needed, eval() the text
  57.         // form of rawest too
  58.         var obj2 = eval ("(" + txt2 + ")");
  59.  
  60.         window.onload = function() {
  61.             // original
  62.             document.getElementById("fname").innerHTML=obj.employees[1].firstName
  63.             document.getElementById("lname").innerHTML=obj.employees[1].lastName
  64.             // display equivalence of eval() of txt2
  65.             get("fname2").innerHTML=obj2.employees[1].firstName
  66.             get("lname2").innerHTML=obj2.employees[1].lastName
  67.             // display equivalence of raw
  68.             get("fnamer").innerHTML=raw.employees[1].firstName
  69.             get("lnamer").innerHTML=raw.employees[1].lastName
  70.             // display equivalence of rawest
  71.             get("fnamerst").innerHTML=rawest.employees[1].firstName
  72.             get("lnamerst").innerHTML=rawest.employees[1].lastName
  73.         };
  74.     </script>
  75. </head>
  76.  
  77. <body>
  78.     <h1>eval() demo</h1>
  79.     <p>
  80.         results of original code (obj):
  81.         <span id="fname"></span> <span id="lname"></span>
  82.     </p>
  83.     <p>
  84.         results of eval() of text form of rawest:
  85.         <span id="fname2"></span> <span id="lname2"></span>
  86.     </p>
  87.     <p>
  88.         results of raw json without eval():
  89.         <span id="fnamer"></span> <span id="lnamer"></span>
  90.     </p>
  91.     <p>
  92.         results of rawest json without eval():
  93.         <span id="fnamerst"></span> <span id="lnamerst"></span>
  94.     </p>
  95. </body>
  96. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement