Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

eval() problem with JSON in IE vs FF/NS 1

Status
Not open for further replies.

dkdude

Programmer
Jun 16, 2003
849
DK
This works fine in FF & NS, but throws an error (str is not defined) in IE:

Code:
var json = {"code": [
  {
   "c": "div class=\"menu\""
   },
  {
   "c": "div id=\"main_file\""
   },
  {
   "c": "File"
   },
  {
   "c": "div id=\"sub_file\"",
   },
  {
   "c": "Open"
   },
  {
   "c": "br /"
   },
  {
   "c": "Save"
   },
  {
   "c": "br /"
   },
  {
   "c": "Exit"
   },
  {
   "c": "/div"
   },
  {
   "c": "/div"
   },
  {
   "c": "/div"
   }
]};


var input = eval(json);
var str = "";

var where=0;
for ( var recno in input.code ) {
  var subcode = input.code[recno];
  for ( var subrec in subcode )
    str += where+" : "+subcode[subrec] + "\n";
  where++;
}

Code:
<html>
<head>
  <title></title>
  <script type="text/javascript" src="demo6.js"></script>
</head>
<body onload="alert(str);">
</body>
</html>

Any ideas?
 
Incidentally, you'd probably have found it easier to spot with better formatting, and better use of single & double quotes:

Code:
var json = {
	'code': [
		{ 'c': 'div class="menu"' },
		{ 'c': 'div id="main_file"' },
		{ 'c': 'File' },
		{ 'c': 'div id="sub_file"' },
		{ 'c': 'Open' },
		{ 'c': 'br /' },
		{ 'c': 'Save' },
		{ 'c': 'br /' },
		{ 'c': 'Exit' },
		{ 'c': '/div' },
		{ 'c': '/div' },
		{ 'c': '/div' }
	]
};

Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks Dan -can't believe I missed a simple typo ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top