Big problem - disapearing code...urgent help needed

R

Rob

Hi everyone,
I'm having some big JS problems - my function is making my html code
vanish, and I need help!

Here's the code, try it for yourself. You'll notice that when you
click either links and then look at the source code, it's all gone,
apart from what the function is writing. How can I make the code just
append the result under the links? I cannot use <div> or <layer> tags
(show/hide). Thoughts??

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<title>Untitled Document</title>
<script language="javascript">

var jsData = new Array();
jsData[0] = {bowl:"I", year:1967, winner:"Packers", winScore:35,
loser:"Chiefs", losScore:10};
jsData[1] = {bowl:"II", year:1968, winner:"Packers", winScore:33,
loser:"Raiders (Oakland)", losScore:14};
jsData[2] = {bowl:"III", year:1969, winner:"Jets", winScore:16,
loser:"Colts (Balto)", losScore:7};
jsData[3] = {bowl:"IV", year:1970, winner:"Chiefs", winScore:23,
loser:"Vikings", losScore:7};
jsData[4] = {bowl:"V", year:1971, winner:"Colts (Balto)", winScore:16,
loser:"Cowboys", losScore:13};


function winner() {

// Step 1 - sort by winner function
function sortByWinner(a, b)
{
a = a.winner.toLowerCase();
b = b.winner.toLowerCase();
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
}

// Step 2 - call the sort by winner function
jsData.sort(sortByWinner);

// Step 3 - print the output of the function
for (var i=0; i<jsData.length; i++)
{
document.write(jsData.year + jsData.winner +"<p>");
}
}

function year() {

// Step 1 - sort by year function
function sortByYear(a, b)
{
return a.year - b.year;
}


// Step 2 - call the sort by year function
jsData.sort(sortByYear);

// Step 3 - print the output of the function
for (var i=0; i<jsData.length; i++)
{
document.write(jsData.year + jsData.winner +"<p>");
}
}

</script>
</head>

<body>
<a href="javascript:winner()">Winner</a><br>
<a href="javascript:year()">Year</a><br>
</body>
</html>
 
L

Lasse Reichstein Nielsen

You'll notice that when you click either links and then look at the
source code, it's all gone, apart from what the function is
writing.

Yes, document.write does that if invoked after the page has finished
loading.
How can I make the code just append the result under the
links?

Either use DOM methods to write into the existing document, or
use document.write to write it before the page finishes loading.
I cannot use <div> or <layer> tags (show/hide).

Why not? It would be perfect: Create two divs in which you generate
HTML with document.write. Then use the links to toggle which one
is visible.
/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top