View Full Version : Changing content randomly with Javascript
dhtmlhelp
08-11-2002, 01:11 PM
Hi,
I would like the content of a specific part of an html page to change randomly. Is there anyway to do this with javascript without having to use server side language or iframes?
DHTMLHELP
Graeme Hackston
08-11-2002, 02:20 PM
http://www.javascriptkit.com/javatutors/randomorder.shtml
The following will randomly show a different message every one second, this can be changed at "repeat time".
The messages are shown wherever the DIV tag is positioned in your page.
To position the Div tag include CSS attributes within the opening DIV tag
style="position:absolute;left:100;top:100"
<script>
<!--
say_what=new Array()
say_what[say_what.length]='A bird in the hand'
say_what[say_what.length]='Finders Keeper'
say_what[say_what.length]='Now what?'
say_what[say_what.length]='So you want to read me eh'
say_what[say_what.length]='WHAT!!!!!!'
say_what[say_what.length]='Yes'
say_what[say_what.length]='Hmm'
function rdm_message(){
rdm=1+Math.floor(Math.random()*say_what.length-1)
odiv.innerHTML="Random Message = "+ say_what[rdm]
setTimeout("rdm_message()",1000) // repeat time
}
setTimeout("rdm_message()",1000)
//-->
</script>
<div id="odiv"></div>
Give it a try, see how it goes.
dhtmlhelp
08-11-2002, 05:16 PM
Hi Mr J,
thanks, but I need to display images as well as a form. Also the content should not change every xxx seconds/time but every visit to that page (maybe randomly pulling content from another html page).
is this possible?
DHTMLHELP
I don't know about pulling content from another page but random onload is possible.
There is a couple of ways to do this depending on which is going to be the most convenient.
You can create an array as in the above script containing the path and names of your images,
OR
Change the name of all your images to something like,
image1.gif
image2.gif and run a script like this:
<SCRIPT>
var imageNo=3 // change for number of images used
num=parseInt(Math.random()*imageNo)
status=num
document.write('<img src="image'+num+'.gif" height=100 width=100 border=2>');
</SCRIPT>
There is a couple of examples on my site which might give you a better idea
www.huntingground.freeserve.co.uk/scripts/rdmimg.htm
Let me know.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.