/    Sign up×
Community /Pin to ProfileBookmark

JSON explaination please

Could someone explain to me why I get an error message
saying that ‘TS’ is not defined in the following code?

I get the error with FF.

IE appears to work, but I would like to make it cross-browser compatible.

[code=php]
<html>
<head>
<title>JSON Test</title>
<script type=”text/javascript”>

TS = { // to be put into separate file at a later time
place : [
{‘ids’:’Alley Oop’, ‘win’:7, ‘lose’:3, ‘draw’:0, ‘tot’:0, ‘pts’:0 },
{‘ids’:’Betty Boop’, ‘win’:6, ‘lose’:4, ‘draw’:1, ‘tot’:0, ‘pts’:0 },
{‘ids’:’Clarabel Cow’, ‘win’:5, ‘lose’:5, ‘draw’:0, ‘tot’:0, ‘pts’:0 },
{‘ids’:’Donald Duck’, ‘win’:4, ‘lose’:6, ‘draw’:1, ‘tot’:0, ‘pts’:0 },
{‘ids’:’Elmer Fudd’, ‘win’:3, ‘lose’:7, ‘draw’:0, ‘tot’:0, ‘pts’:0 }
]
}
// Note: No ‘,’ at end of last entries

function Display_Stats() {

// FUNCTION FOR STATS
for (var i=0; i<TS.place.length; i++) {
TS.place[i].tot = TS.place[i].win + TS.place[i].lose + TS.place[i].draw;
TS.place[i].pts = (3 * TS.place[i].win) + TS.place[i].draw;
}

var common_site = ‘Player: ‘;

document.write(‘<table border=”1″><tr><td width=”34″>&nbsp;</td>’);
document.write(‘<td width=”236″>&nbsp;Player </td>’);
document.write(‘<td width=”34″>Win</td><td width=”34″>Lose</td><td width=”34″>Draw</td>’);
document.write(‘<td width=”34″>Total</td><td width=”34″>PTS</td></tr>’);

for (var i=0; i<TS.place.length; i++) {
document.write(‘<tr><td>’+(i+1)+'</td>’);
site = common_site + (i+1) + ‘ : ‘ + TS.place[i].ids;
document.write(‘<td>’+ site + ‘</td>’);
document.write(‘<td>’ + TS.place[i].win + ‘</td>’);
document.write(‘<td>’ + TS.place[i].lose + ‘</td>’);
document.write(‘<td>’ + TS.place[i].draw + ‘</td>’);
document.write(‘<td>’ + TS.place[i].tot + ‘</td>’);
document.write(‘<td>’ + TS.place[i].pts + ‘</td></tr>’);
}

// CLOSE TABLE
document.write(‘</table>’);
}
</script>
</head>
<body onLoad=”Display_Stats()”>
<p />
JSON Test
</body>
</html>
[/code]

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@mrhooMay 12.2007 — When you call document.write after the page has loaded, firefox wants a document.close. It would be happier if you gave it the basic page elements as well- html, head, title,body. Or run it before the page loads completely.

// CLOSE TABLE

document.write('</table>');

[B]document.close()[/B]
Copy linkTweet thisAlerts:
@JMRKERauthorMay 12.2007 — Thanks for the tips 'mrhoo', I'll give that a shot.

I thought the page was loaded by the <body onLoad='Display_Stats()'> tag

and that the other basic page elements were already accounted for.

Appreciate the help.
Copy linkTweet thisAlerts:
@JMRKERauthorMay 12.2007 — For those who may be interested, this is my solution.

[code=php]
<html>
<head>
<title>JSON Test</title>
<script type="text/javascript">

TS = { // to be put into separate file at a later time
place : [
{'order':0, 'ids':'Alley Oop', 'win':7, 'lose':3, 'draw':0, 'tot':0, 'pts':0 },
{'order':0, 'ids':'Betty Boop', 'win':6, 'lose':4, 'draw':1, 'tot':0, 'pts':0 },
{'order':0, 'ids':'Clarabel Cow', 'win':5, 'lose':5, 'draw':0, 'tot':0, 'pts':0 },
{'order':0, 'ids':'Donald Duck', 'win':4, 'lose':6, 'draw':1, 'tot':0, 'pts':0 },
{'order':0, 'ids':'Elmer Fudd', 'win':3, 'lose':7, 'draw':0, 'tot':0, 'pts':0 }
]
}
// Note: No ',' at end of last entries

function sortNumber(a,b) { return b - a; } // reverse order number sort modification

function Display_Stats() {

var TS_Order = new Array();

// FUNCTION FOR STATS
for (var i=0; i<TS.place.length; i++) {
TS.place[i].tot = TS.place[i].win + TS.place[i].lose + TS.place[i].draw;
TS.place[i].pts = (3 * TS.place[i].win) + TS.place[i].draw;
// want to use 'i' as pointer to correct 'place' element
TS_Order.push((TS.place[i].pts*1000)+i);

}
TS_Order = TS_Order.sort(sortNumber);

var tmp = '';
tmp += '<table border="1"><tr><td width="34">Order</td>';
tmp += '<td width="136">&nbsp;Player </td>';
tmp += '<td width="34">Win</td><td width="34">Lose</td><td width="34">Draw</td>';
tmp += '<td width="34">Total</td><td width="34">PTS</td></tr>';

j = 0;
while (j<TS_Order.length) {
i = TS_Order[j] % 1000;
tmp += '<tr><td>' + (j+1) +'</td>';
tmp += '<td>'+ TS.place[i].ids + '</td>';
tmp += '<td>' + TS.place[i].win + '</td>';
tmp += '<td>' + TS.place[i].lose + '</td>';
tmp += '<td>' + TS.place[i].draw + '</td>';
tmp += '<td>' + TS.place[i].tot + '</td>';
tmp += '<td>' + TS.place[i].pts + '</td></tr>';
j++;
}
tmp += '</table>';
document.getElementById('stbl').innerHTML = tmp;
}
</script>
</head>
<body onLoad="Display_Stats()">
<h1>JSON Element Test</h1>
<div id="stbl"></div>
</body>
</html>
[/code]

Try changing the 'Win', 'Lose', or 'Draw' values to see the order change.

Any improvements will be considered.

Thanks again 'mrhoo'.
Copy linkTweet thisAlerts:
@rootMay 12.2007 — Question, WHY are you calling it a JSON test when all your using is standard Javascript?
Copy linkTweet thisAlerts:
@JMRKERauthorMay 12.2007 — Ignorance is my only excuse. :o

It is an initial attempt at using this kind of code, hence a ....Test.html

I thought the set-up and function use of the 'TS' section was a JSON format.

Call it something else if you want ... I just wanted it to work!

I modified the logic used here on another forum posting,

so the JSON nomenclature is probably wrong there too.
Copy linkTweet thisAlerts:
@felgallMay 12.2007 — JavaScript Object Notation (JSON) is just one way of defining objects in JavaScript hence the name JavaScript Object Notation.

JSON is part of JavaScript.
Copy linkTweet thisAlerts:
@JMRKERauthorMay 13.2007 — I made some modifications for '.' comments.

Does the inclusion of the function FormTotal() and FormPTS() in the TS definition

better meet the qualifications to call it a JSON attempt or is it still 'standard JavaScript'?

[code=php]
<html>
<head>
<title>JSON (Questionable) Element Test</title>
<script type="text/javascript">

TS = { // to be put into separate file at a later time
place : [
{'order':0, 'ids':'Alley Oop', 'win':7, 'lose':3, 'draw':0, 'tot':0, 'pts':0 },
{'order':0, 'ids':'Betty Boop', 'win':6, 'lose':4, 'draw':1, 'tot':0, 'pts':0 },
{'order':0, 'ids':'Clarabel Cow', 'win':5, 'lose':5, 'draw':0, 'tot':0, 'pts':0 },
{'order':0, 'ids':'Donald Duck', 'win':4, 'lose':6, 'draw':1, 'tot':0, 'pts':0 },
{'order':0, 'ids':'Elmer Fudd', 'win':3, 'lose':7, 'draw':0, 'tot':0, 'pts':0 }
],
FormTotal : function(locn) {
this.place[locn].tot = this.place[locn].win + this.place[locn].lose + this.place[locn].draw;
},
FormPTS : function(locn) {
this.place[locn].pts = (3 * this.place[locn].win) + this.place[locn].draw;
}
}
// Note: No ',' at end of last entries

function sortNumber(a,b) { return b - a; } // reverse order number sort modification

function Display_Stats() {

var TS_Order = new Array();

// FUNCTION FOR STATS
for (var i=0; i<TS.place.length; i++) {
TS.FormTotal(i);
TS.FormPTS(i);
// want to use 'i' as pointer to correct 'place' element
TS_Order.push((TS.place[i].pts*1000)+i);

}
TS_Order = TS_Order.sort(sortNumber);

var tmp = '';
tmp += '<table border="1"><tr><td width="34">Order</td>';
tmp += '<td width="136">&nbsp;Player </td>';
tmp += '<td width="34">Win</td><td width="34">Lose</td><td width="34">Draw</td>';
tmp += '<td width="34">Total</td><td width="34">PTS</td></tr>';

j = 0;
while (j<TS_Order.length) {
i = TS_Order[j] % 1000;
tmp += '<tr><td>' + (j+1) +'</td>';
tmp += '<td>'+ TS.place[i].ids + '</td>';
tmp += '<td>' + TS.place[i].win + '</td>';
tmp += '<td>' + TS.place[i].lose + '</td>';
tmp += '<td>' + TS.place[i].draw + '</td>';
tmp += '<td>' + TS.place[i].tot + '</td>';
tmp += '<td>' + TS.place[i].pts + '</td></tr>';
j++;
}
tmp += '</table>';
document.getElementById('stbl').innerHTML = tmp;
}
</script>
</head>
<body onLoad="Display_Stats()">
<h1>JSON Element Test</h1>
<div id="stbl"></div>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@rootMay 13.2007 — TBH I dont care what they call it, it to me looks OO more than anything but then again, thats the computer industry as a whole, one minute its Javascript then its DHTML and then its Ajax and then JSON, all jargoneese which I personally hate as theirs no need to dress up something that is already javascript and title it or market it under a different banner.

Its like that Joomla & Suckerfish stuff just rebadge of an existing language, just because something is laid out in a particular fashion doesnt mean anyting at all. I just dont understand the need for it.
Copy linkTweet thisAlerts:
@JMRKERauthorMay 13.2007 — I just dont understand the need for it.[/QUOTE]
OK, I can agree with that. It's easier to understand and remember if they don't keep changing the names of the same things every other year.

I was obviously trying to be too PC (politically correct) and thought I might get more hits on my question if I was as specific as possible.
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...