JavaScript help needed (YQL and JavaScript)

I have this "adapted" javascript code to display funfacts about anything (in this case about movies and/or History) However, it is built to display only one fact at a time, the user needs to press a button to display another funfact.

My question is, is there a way to display all the facts at once and not just one at a time?

function funfacts(o){
  var facts = document.getElementById('funfacts');
  facts.className = 'js';
  if(facts){
    var data = o.query.results.p;
    var link = facts.getElementsByTagName('a')[0];
    link.innerHTML = '(see all facts)';
    var out = document.createElement('p');
    out.className = 'fact';
    facts.insertBefore(out,link.parentNode);
    function seed(){
      var ran = parseInt(Math.random()*data.length);
      out.innerHTML = data[ran];
    }
    var b = document.createElement('button');
    b.innerHTML = 'get another fact';
    b.onclick = seed;
    link.parentNode.insertBefore(b,link);
    seed();
  }
}

  <script src="http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D'http%3A%2F%2Fwww.tealdragon.net%2Fhumor%2Ffacts%2Ffacts.htm'%20and%20xpath%3D'%2F%2Fli%2Fp'&format=json&diagnostics=true&callback=funfacts"></script>  

Answers


The variable data contains all of the facts in an array. When the user clicks, the function seed is randomly selecting one item out of the array.

So, to display them all:

out.innerHTML = data.join("<br />");

This joins the facts into a single string, separated by newlines.

Edited to add:

Here's an example of a complete, very simple web page that displays all of the items:

<html>
<body>
<div id="funfacts">
</div>
<script>
function funfacts(o){
  var facts = document.getElementById('funfacts');
  if(facts){
    var data = o.query.results.p;
    var out = document.createElement('p');
    out.className = 'fact';
    facts.appendChild(out);
    out.innerHTML = data.join("<br />");
  }
}
</script>
<script src="http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D'http%3A%2F%2Fwww.tealdragon.net%2Fhumor%2Ffacts%2Ffacts.htm'%20and%20xpath%3D'%2F%2Fli%2Fp'&format=json&diagnostics=true&callback=funfacts"></script>

</body>
</html>

Need Your Help

How to load NSURL that contains “#” with WebView?

cocoa webview

I have a URL string which contains "#".For example,

Mixed Content Error in IE7 with html5shiv and SSL

asp.net html5 ssl internet-explorer-7 html5shiv

I'm developing an ASP.Net app that is using html5shiv and ssl. For some reason I am getting mixed content errors in IE7 using html5shiv. If I remove html5shiv the errors go away. I'm also using ...

How to compare two big XML files item by item efficiently?

c# .net xml c#-2.0 compare

I plan to implement an method to compare two big XML files (but less than 10,000 element lines for each of other).