JavaScript help needed (YQL and JavaScript) -
i have "adapted" javascript code display funfacts (in case movies and/or history) however, built display 1 fact @ time, user needs press button display funfact.
my question is, there way display facts @ once , not 1 @ 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 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 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>
the variable data
contains of facts in array. when user clicks, function seed
randomly selecting 1 item out of array.
so, display them all:
out.innerhtml = data.join("<br />");
this joins facts single string, separated newlines.
edited add:
here's example of complete, simple web page displays of 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>
Comments
Post a Comment