Hello, im making a fact system, and I need a refresh script, For example when someone clicks on the "next fact" button, the "div" part (where the fact is displayed) refreshes. ONLY the div part. and the javascript below is externally loaded...

/ ==============================================
// Copyright 2004 by CodeLifter.com
// Free for all; but please leave in this header.
// ==============================================

var Fact=new Array() // do not change this!

// List of facts!

Fact[0] = "You are born with 300 bones but only have 206 as an adult because many of your bones fuse together and make bigger more solid bones.";
Fact[1] = "The word "queue" is the only word in the English language that is still pronounced the same way when the last four letters are removed.";
Fact[2] = "Beetles taste like apples, wasps like pine nuts, and worms  like fried bacon.";
Fact[3] = "In 1386, a pig in France was executed by public hanging for the murder of a child";
Fact[4] = "Scientist have believed that there are more chicken in the world than any human
beings.";


// ======================================
// Generate the facts
// ======================================
var Q = Fact.length;
var whichFact=Math.round(Math.random()*(Q-1));
function showFact(){document.write(Quotation[whichFact]);}
showFact();
</script>




============================================================

Recommended Answers

All 2 Replies

That is not really difficult, and pretty funny someone "copyrighted" this piece of extremely difficult code. Anyways, you can change the contents of a div by adding an ID to it and then calling it with document.getElementById().

Example:

<script type='text/javascript'>
function ChangeMyDiv() {
document.getElementById('MyDiv').innerHTML = 'This is some other text!';
}
</script>
<div id='MyDiv'>This is some text!</div>
<a href='#' onclick='ChangeMyDiv(); return false;'>Change MyDiv</a>

Also, please be aware that the (crappy) code you got there, has a function that writes the fact in the document, instead of changing the innerHTML or innerText of a element. If showFact is called when the document has already been loaded (by an event or something), the page will be blank except for the fact.

~G

Fact[1] = "The word "queue" is the only word in the English language that is still pronounced the same way when the last four letters are removed.";

It isn't obvious to me how that code could ever work; that string is malformed.

Anyway, this

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
  <head>
    <meta name="generator" content=
    "HTML Tidy for Windows (vers 25 March 2009), see www.w3.org">
    <style type="text/css">
	#factoid {
        width: 160px;
        white-space: pre-wrap; 
        white-space: -moz-pre-wrap; 
	  white-space: -pre-wrap; 
	  white-space: -o-pre-wrap; 
	  word-wrap: break-word; 
	}
    </style>

    <script type="text/javascript">
        function changeIt(){
            document.getElementById('factoid').innerHTML = msg[Math.floor(msg.length*Math.random())]
        }
	  var msg = [ "You are born with 300 bones but only have 206 as an adult because many of your bones fuse together and make bigger more solid bones." , 
	    "The word 'queue' is the only word in the English language that is still pronounced the same way when the last four letters are removed." , 
	    "Beetles taste like apples, wasps like pine nuts, and worms like fried bacon." , 
	    "In 1386, a pig in France was executed by public hanging for the murder of a child" , 
	    "Scientist have believed that there are more chicken in the world than any human beings." ]
    </script>
    <title></title>
  </head>
  <body>
    <div onclick="changeIt()" id='factoid'>
      click here
    </div>
  </body>
</html>

is far simpler.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.