Topic: JSON eval() problems with IE Pages that link to <a href="https://ozoneasylum.com/backlink?for=29265" title="Pages that link to Topic: JSON eval() problems with IE" rel="nofollow" >Topic: JSON eval() problems with IE\

 
Author Thread
beaner
Bipolar (III) Inmate

From: LA, CA, USA
Insane since: Apr 2002

IP logged posted posted 06-07-2007 01:24 Edit Quote

I'm trying to load a Yahoo widget DataTable by using some JSON data that I have created myself dynamically. I can get the DataTable to load and the example works in Firefox, but it won't work in IE.

When I load the page, the DataTable says "Loading Data...", and I get an error within the browser
line 773: 'undefined' is null or not an object

note: the code does not exceed line 140

The code is appended:

code:
<script type="text/javascript">

var sample_input_data = "0.0,1.0:1.0,21.0:2.0,321.0:3.0,2005.0:4.0,7737.0:5.0,22461.0:6.0,54121.0:7.0,114381.0:8.0,219345.0:9.0,390277.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:";

var myColumnHeaders = [ {key:"areacode",className:"col1"}, {key:"state",className:"col2"}];

var myColumnSet = new YAHOO.widget.ColumnSet(myColumnHeaders);

var myDataSource2 = new YAHOO.util.DataSource(formatStringToJSON(sample_input_data));

myDataSource2.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;

myDataSource2.responseSchema = { fields: ["areacode","state"]	};

var myDataTable = new YAHOO.widget.DataTable("scroll",myColumnSet,myDataSource2,{scrollable:true});


function formatStringToJSON(inputString)
{
	var point_sets = inputString.split(":");
	var json_text = "";

	for (x = 0; x < point_sets.length-1; x++)
	{
		var point = point_sets[x].split(",");
		json_text += "{ x: " + point[0] + ", y: " + point[1] + "}, ";
	}

	json_text = '( [' + json_text + '] )';

	//var data_points = eval ( ' { points: [' + json_text + '] }');
	var data_points = eval( '{ areacodes: [ { areacode: \"201\", state: \"New Jersey\"}, ] }' );
	
	return data_points;
}

</script>



I originally populated the DataTable using a static structure, but now I need to populate it dynamically so I'm doing this using a string and using eval().

Does anyone have an idea why it won't work in IE.

Or is there another way of doing this?

poi
Paranoid (IV) Inmate

From: Norway
Insane since: Jun 2002

IP logged posted posted 06-07-2007 03:10 Edit Quote

Shouldn't this be ?

code:
return eval( 'data_points={ areacodes: [ { areacode: \"201\", state: \"New Jersey\"}, ] }' );


I've only tried in the address bar of this window, but that's the only way I could alert( data_points.areacodes[0].state )

beaner
Bipolar (III) Inmate

From: LA, CA, USA
Insane since: Apr 2002

IP logged posted posted 06-08-2007 00:59 Edit Quote

Ok, so I played with it some more taking into account your advice. I got some interesting results.

code:
var sample_input_data = "0.0,1.0:1.0,21.0:2.0,321.0:3.0,2005.0:4.0,7737.0:5.0,22461.0:6.0,54121.0:7.0,114381.0:8.0,219345.0:9.0,390277.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:10.0,654321.0:";

var myColumnHeaders2 = [ 
	{key:"x",className:"col1"}, 
	{key:"y",className:"col2"}
];


var myColumnSet2 = new YAHOO.widget.ColumnSet(myColumnHeaders2);

var jsonPoints = formatStringToJSON(sample_input_data);
alert(jsonPoints.data_points)
var myDataSource2 = new YAHOO.util.DataSource(jsonPoints.data_points);
	
myDataSource2.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;

myDataSource2.responseSchema = { fields: ["x","y"] };

var myDataTable2 = new YAHOO.widget.DataTable("scroll",myColumnSet2,myDataSource2,{scrollable:true});

function formatStringToJSON(inputString)
{
	var point_sets = inputString.split(":");
	var json_text = "";

	for (x = 0; x < point_sets.length-1; x++)
	{
		var point = point_sets[x].split(",");
		json_text += "{ x: \"" + point[0] + "\", y: \"" + point[1] + "\"}, ";
	}
		
	return eval( 'data_points = { points: [' + json_text + '] } ');  // return 1.
	//return eval( ' { points: [' + json_text + '] } ');             // return 2.
}



When using return line 1
It neither works in FF nor IE saying "Loading data..."
However, there are no errors in IE or FF (using firebug)

When using return line 2 (and changing the alert statement and declaration of the Yahoo Datasource)
It works in FF, but does not work in IE, stating "Loading data.." and there is an error in the code in IE only on line 774.

Quite peculiar. Thanks for your response.

(Edited by beaner on 06-08-2007 01:01)

poi
Paranoid (IV) Inmate

From: Norway
Insane since: Jun 2002

IP logged posted posted 06-08-2007 11:26 Edit Quote

you realize that saying things like " there is an error in the code in IE only on line 774 ", doesn't help me/us helping you at all if we don't get to see what's on the line 774 and before, the inputs, the call trace, or better the whole page.



Post Reply
 
Your User Name:
Your Password:
Login Options:
 
Your Text:
Loading...
Options:


« BackwardsOnwards »

Show Forum Drop Down Menu