Log in

View Full Version : How do I make random sentences appear when page refreshes



thinandpale
08-22-2006, 09:24 AM
I'm not sure if I need php for this to work, but I've seen it done with seperate JS files in the same directory.

I've been looking at other sites to see how they do it, but I can never view the changing script! What do I need to put on the page to make the changing script appear on the page?

DimX
08-22-2006, 10:01 AM
Put that into the HEAD section of your page:


<script type="text/javascript">
var sentences = new Array(
"Put your sentences here.",
"Quoted and comma delimited.",
"Third sentence.",
"Fourth sentence.",
"And so on...",
"<em>HTML tags may be used</em>",
"Use <br /> for line breaks.",
"NO COMMA AFTER THE LAST ONE!!!"
);
</script>

And that, where you want the sentences to appear:


<script type="text/javascript">
document.write(sentences[Math.floor(Math.random()*sentences.length)]);
</script>

thinandpale
08-22-2006, 10:17 AM
It didn't work! Nothing shows up on the page!

jscheuer1
08-22-2006, 10:26 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title>Random Sentence w/Cookie script - Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<span id="theS"></span>
<script type="text/javascript">
/* Random Sentence w/Cookie script
* All credits must remain for legal use */

// Cookie code from: http://www.quirksmode.org/js/cookies.html
function createCookie(name,value) {
document.cookie = name+"="+value+"; path=/";
}

function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

function rs(){ // Random Sentence on Refresh w/Cookie - jscheuer1 - http://www.dynamicdrive.com/forums
var s=[
'Hey there',
'How there',
'Hey now',
'Way home',
'All now'];
s.sort(function() {return 0.5 - Math.random();}) //thanks to Mike (aka Mwinter) :)
var sn=readCookie('sen')&&readCookie('sen')==s[0]? s[1] : s[0];
document.getElementById('theS').innerHTML=sn;
createCookie('sen',sn)
}
rs();
</script>
</body>
</html>

DimX
08-22-2006, 10:55 AM
It didn't work! Nothing shows up on the page!
Whoops, sorry, my bad, there was a typo in the code :p try it again, it works now. ;)

thinandpale
08-22-2006, 11:21 AM
Yeah, that was it......... that worked!

Thank you very much. I've been searching the whole web for the last 20 hours trying to figure this out. I read pages after pages of .js and .php and other message boards weren't able to help.

Thanks for helping me.

I was trying to use the Array method, but this works.

Is there a way for me to use mp3 embed tags instead of sentences ?

or do I need to make a seperate page listing the mp3 files and then if so what do I do to get those mp3 songs to play at random when the page refreshes.

thinandpale
08-22-2006, 11:35 AM
I made a sample page for both scripts. They both work.

Is there a way for me to use mp3 files ?

blm126
08-22-2006, 12:39 PM
Where ever you put the string to display put the HTML for your mp3.

thinandpale
08-23-2006, 02:06 AM
I just figured it out....

I needed to put \ infront of " in the embed so it could read it

jscheuer1
08-23-2006, 02:55 AM
Thought that I would just throw in here that my version, because it uses cookies, is much less likely to ever randomly repeat. Cookies would have to be disabled for that to happen.