PDA

View Full Version : Help - Random Number script..



kamisaibou
03-02-2006, 05:46 AM
I'm not sure if this is working at all...
I'm trying to get the iframe to display a different poem I've written each time the page is loaded or refreshed. Am I doing this right? :confused:
Thank you for looking!!
(There are poems in the real text documents I have, but it makes the file-size smaller without them in there...)



<html><body onload="ChangePoem()">

<div style="text-align: center">

<p style="color: blue">Message here<br /><br /></p>


<table style="text-align: center; background-color: rgb(245, 225, 227);" border="1"
cellpadding="5" width="100%">


<!-- Random Poem --><tr>
<td width="72%">
<iframe id="Poem" src="Poem 1.txt" width="96%"></iframe>
<script language="text/javascript">
var Poem(3);
var Last=3;
Poem[0]="Poem 1.txt";
Poem[1]="Poem 2.txt";
Poem[2]="Poem 3.txt";
function ChangePoem() {
var ranNum = Math.floor(Math.random() * (lastPoem - startPoem)) + startPoem;
document.getElementById ("Poem").location.href = Poem[ranNum];
}
</script>
</td></tr>


</table></div>
</body></html>

TheJoshMan
03-02-2006, 07:14 AM
My suggestion would be to use the following script that can be found here...
http://www.dynamicdrive.com/dynamicindex17/randomiframe.htm

I have tested this one and it works fine... Here is a copy of the code...




<html><body>

<div style="text-align: center">

<p style="color: blue">Message here<br /><br /></p>


<table style="text-align: center; background-color: rgb(245, 225, 227);" border="1"
cellpadding="5" width="100%">


<!-- Random Poem --><tr>
<td width="72%">
<script language="JavaScript1.2">

//Random iframe content- Dynamic Drive (www.dynamicdrive.com)
//For full source code, and Terms Of use, visit http://dynamicdrive.com
//This credit MUST stay intact for use

var ie=document.all&&navigator.userAgent.indexOf("Opera")==-1
var dom=document.getElementById&&navigator.userAgent.indexOf("Opera")==-1

//Specify IFRAME display attributes
var iframeprops='width="97%" marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="1" scrolling="no"'

//Specify random URLs to display inside iframe
var randomcontent=new Array()
randomcontent[0]="Poem1.txt"
randomcontent[1]="Poem2.txt"
randomcontent[2]="Poem3.txt"


//No need to edit after here
if (ie||dom)
document.write('<iframe id="dynstuff" src="" '+iframeprops+'></iframe>')

function random_iframe(){
if (ie||dom){
var iframeobj=document.getElementById? document.getElementById("dynstuff") : document.all.dynstuff
iframeobj.src=randomcontent[Math.floor(Math.random()*randomcontent.length)]
}
}

window.onload=random_iframe

</script>


</td></tr>


</table></div>
</body></html>



If you notice that code writes the iframe from within the script itself instead of using one that is already written into the HTML. Hope this helps.

kamisaibou
03-02-2006, 10:46 PM
Thank you, that works!! ^_^ I very much appreciate your help... :)