PDA

View Full Version : Resolved Math makes my head hurt, anyway. =/



FailureAtJavascript
06-07-2010, 03:32 AM
Greetings folks, I hope you're having a good day.

I've recently added some silly musings to my site on page load, I did this with PHP.. unfortunately they were such a hit that everybody just sits there refreshing the main page over and over for hours on end (literally) to see the next message. As you can imagine, even with cookies this is quite volatile on the bandwidth, so I searched for a way to only be able to reload a div, or rather hopefully a <p>aragraph. -- Google tells me Javascript is my savior!

So I'm trying (failing) my hand at Javascript~ I found a script on the internetter-ma-bob that for the most part does the trick, and I actually got to work in any semblance (99% sure I'm just an idiot), but I was hoping one of you could assist me in tinkering it to do one little thing.

It loads a random message on page load, and with a button you can change the message it says without reloading the entire page. Unfortunately, when you press the button, it just picks the next message in the list. I'd like it to pick a random message from the list every time the button is clicked.


var Statements = new Array(

'I like turtles.'
'I like tortoises.'
'I like terrapins.'

);



function GetStatement(outputtype)
{
if(++Number > Statements.length) Number = 0;
if (outputtype==0)
document.write(Statements[Number])
else if (document.getElementById)
document.getElementById("ran-dumb").innerHTML=Statements[Number];
}



function GetRandomNumber(lbound, ubound)
{
return (Math.floor(Math.random() * (ubound - lbound)) + lbound);
}


var Number = GetRandomNumber(0, Statements.length);



<script src="ran-dumb.js" language="javascript" type="text/javascript"></script>

<p id="ran-dumb">
<script type="text/javascript">
GetStatement(0)
</script>
</p>

<p>
<button style="background-color:#220C19;color:#DB44DB;" onClick="GetStatement(1)">New Quote!</button>
</p>


Thank you in advance for your time and any assistance you may give,
~FailureAtJavascript

vwphillips
06-07-2010, 09:13 AM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<script type="text/javascript">
/*<![CDATA[*/
var Statements =[

'I like turtles.',
'I like tortoises.',
'I like terrapins.'

];



function GetStatement(){
var Number=Math.floor(Math.random()*Statements.length);
if (Number!=this.nu){
document.getElementById("ran-dumb").innerHTML=Statements[Number];
this.nu=Number;
}
else {
GetStatement();
}
}



/*]]>*/
</script></head>

<body>
<p id="ran-dumb">
<script type="text/javascript">
GetStatement(0)
</script>
</p>

<p>
<button style="background-color:#220C19;color:#DB44DB;" onClick="GetStatement(1)">New Quote!</button>
</p>
</body>

</html>

FailureAtJavascript
06-08-2010, 03:18 AM
Much appreciated good sir!~ This is exactly what I was hoping for.

You are a prince among Javascript coders. =P