Which Javascript?
Hi,
I am looking for the name of some JavaScript that I have used before, but I can't remember it. Basically it allow you to have several different messages in the code, but will only show one of them and when you press refresh it will randomly select another one for you view.
Can anyone tell me what this is called or give me an address where I can find the code? Thanks.
Roxy posted this at 12:58 — 5th May 2009.
They have: 17 posts
Joined: Mar 2009
I have found this code which is kind of what I want but I want it to change when you refresh the page, rather than after a set time period:
<script type="text/javascript">
if (document.all || document.getElementById){ //if IE4 or NS6+
document.write('<style type="text/css">\n');
document.write('.dyncontent{display: none; width: 250px; height: 60px;}\n');
document.write('</style>');
}
var curcontentindex=0;
var messages=new Array();
function getElementByClass(classname){
var inc=0;
var alltags=document.all? document.all : document.getElementsByTagName("*");
for (i=0; i<alltags.length; i++){
if (alltags[i].className==classname)
messages[inc++]=alltags[i];
}
}
function rotatecontent(){
//get current message index (to show it):
curcontentindex=(curcontentindex<messages.length-1)? curcontentindex+1 : 0;
//get previous message index (to hide it):
prevcontentindex=(curcontentindex==0)? messages.length-1 : curcontentindex-1;
messages[prevcontentindex].style.display="none"; //hide previous message
messages[curcontentindex].style.display="block"; //show current message
}
window.onload=function(){
if (document.all || document.getElementById){
getElementByClass("dyncontent");
setInterval("rotatecontent()", 2000);
}
}
</script>
Dynamic Content:
<div class="dyncontent" style="display: block">First scroller content (visible)</div>
<div class="dyncontent">Second scroller content</div>
<div class="dyncontent">Third scroller content</div>
Roxy posted this at 14:12 — 5th May 2009.
They have: 17 posts
Joined: Mar 2009
I have found this code which rotates images, but I want it to rotate different DIVs and Scripts. Any ideas?
<script language="JavaScript">
<!--
function random_imglink(){
var myimages=new Array()
myimages[1]="http://www.java-scripts.net/javascripts/img1.gif"
myimages[2]="http://www.java-scripts.net/javascripts/img2.gif"
myimages[3]="http://www.java-scripts.net/javascripts/img3.gif"
var imagelinks=new Array()
imagelinks[1]="http://www.wsabstract.com"
imagelinks[2]="http://www.dynamicdrive.com"
imagelinks[3]="http://www.java-scripts.net"
var ry=Math.floor(Math.random()*myimages.length)
if (ry==0)
ry=1
document.write('<a href='+'"'+imagelinks[ry]+'"'+'><img src="'+myimages[ry]+'" border=0></a>')
}
random_imglink()
//-->
</script>
Megan posted this at 14:55 — 6th May 2009.
She has: 11,421 posts
Joined: Jun 1999
Do you write any javascript or PHP yourself? This would probably be reasonably easy to code yourself, either pulling from an XML file or a database using PHP. It would be a useful starter project if you want to get into that.
Are the messages short (one sentence) or are they more complicated (several paragraphs and a header)? That would influence how you would do this.
Here's a tutorial that might help:
http://www.webdevelopersnotes.com/tips/html/random_text_display_using_ja...
Megan
Connect with us on Facebook!
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.