Simple text link rotator?

Erik

Regular Member
Joined
Dec 14, 2008
Messages
302
Reaction score
415
O.K. I looked around and I can't seem to find this.

Basically I have a 'Click Here' Link attached to a Url. What I'd like to do is find a way to make that link rotate.

In other words Person A clicks on a link and goes to URL 1 on click. Person B goes to URL 2 on click, etc.

Random would also be better.

Can anyone help me out with this?
 
Here is Javascript that will allow you to do it, just replace the href with your link and the text. You can also make it rotate to images or whatever...if you need to know howto do that just let me know. Oh yeh, it goes in your HEAD tags

Code:
<script language="JavaScript1.2">
var howOften = 2; //number often in seconds to rotate
var current = 0; //start the counter at 0
var ns6 = document.getElementById&&!document.all; //detect netscape 6
// place your images, text, etc in the array elements here
var items = new Array();
    items[0]="<a href='yourlink'>Text with a link</a>"; //each link
    items[1]="<a href='yourlink'>Text with a link</a>"; //link 2 and so on
    items[2]="<a href='yourlink'>Text with a link</a>";
    items[3]="<a href='yourlink'>Text with a link</a>";
    items[4]="<a href='yourlink'>Text with a link</a>";
function rotater() {
    if(document.layers) {
        document.placeholderlayer.document.write(items[current]);
        document.placeholderlayer.document.close();
    }
    if(ns6)document.getElementById("placeholderdiv").innerHTML=items[current] 
        if(document.all)
            placeholderdiv.innerHTML=items[current];
    current = (current==items.length) ? 0 : current + 1; //increment or reset
    setTimeout("rotater()",howOften*1000);
}
window.onload=rotater;
//->
</script>
 
Last edited:
dupe post- remove
 
Last edited:
hey thanks for the help, If i put this code in my header and fill in the urls there, what do I need to place in the body to display those rotating links?
 
Here use this, cause to much to explain changing the placeholder
Code:
<script language="JavaScript1.2">
/*
Rotating image or text(You can use for changing banners)
Author: Narayan Chand Thakur
Source: 
This may be used freely as long as this message is intact.
*/
<!--
//you may add your image file or text below
var item=new Array()
item[0]="<a href='#'><img src='ballon2.gif' border='0'></a>"
item[1]="<a href='#'><img src='ballon3.gif' border='0'></a>"
item[2]="<a href='#'><img src='ballon4.gif' border='0'></a>"
item[3]="<strong><font face='arial' size='4' color='red'>Text without a Link!</font></strong>"
item[4]="<a href='#'><font face='arial' size='4' color='darkgreen'><B>This text has Link</B></font></a>"
item[5]="<a href='#'><font face='arial' size='4' color='darkgreen'><B>How do you like that?</B></font></a>"
var current=0
var ns6=document.getElementById&&!document.all
function changeItem(){
if(document.layers){
document.layer1.document.write(item[current])
document.layer1.document.close()
}
if(ns6)document.getElementById("div1").innerHTML=item[current]
{
if(document.all){
div1.innerHTML=item[current]
}
}
if (current==5) current=0
else current++
setTimeout("changeItem()",2000)
}
window.onload=changeItem
//-->
</script>

and then put this in your body, remember to change the left and right so its inthe position you want.
Code:
<layer id="layer1" left="210" top="250" ></layer>
<div id="div1" style="position:absolute;left:210;top:250">
</div>
 
Thank you sir. this should work perfectly
 
i can write up a quick php version if you need it...
 
Here is php one if you need it.

Code:
<?php
//Add as many links you want
$mylink[1] = '<a href="YOURLINK">YOUTEXT</a>';
$mylink[2] = '<a href="YOURLINK">YOURTEXT</a>';
$mylink[3] = '<a href="YOURLINK">YOURTEXT</a>';
//$mylink[4] = .....
//$mylink[5] = ....
// this will count your links itself and select a random one
$id = rand(1,count($mylink));
// this will display the random link
echo $mylink[$id];
?>
 
dude the second option worked wonders for me! Thank you very very very much!
 
No problem, just hit me up if you need any other kind of code....if i dont know i can make it...if i cant make it...i can find it ;)
 
I am looking for something similiar but I would need it to just show random webpages...as in it would just be a redirect i guess to a list of sites and chosen at random...

Does that make sense? Like it would go to yoursite.com/links.html but its really showing other sites chosen from a list of your choosing...

I guess its called a link rotator or something...
 
Not sure if this is what you mean but here it is, it will load a random page upon opening the page that is code is in.

Code:
<head>
<script type="text/javascript" language="javascript">
var randomlinks=new Array("[URL]http://DOMAIN.com[/URL]", "[URL]http://DOMAIN.com[/URL]", "[URL]http://DOMAIN.com[/URL]", "[URL]http://DOMAIN.com[/URL]", "[URL]http://www.DOMAIN.com[/URL]");
location.href = randomlinks[Math.floor(Math.random()*randomlinks.length)];
</script>
</head>
 
is there a way to change this from random to just a regular rotate? example site 1 site 2 site 3 site 1 site 2 site 3 etc. Does anybody have a regular url rotator script?

thanks



Not sure if this is what you mean but here it is, it will load a random page upon opening the page that is code is in.

Code:
<head>
<script type="text/javascript" language="javascript">
var randomlinks=new Array("[URL]http://DOMAIN.com[/URL]", "[URL]http://DOMAIN.com[/URL]", "[URL]http://DOMAIN.com[/URL]", "[URL]http://DOMAIN.com[/URL]", "[URL]http://www.DOMAIN.com[/URL]");
location.href = randomlinks[Math.floor(Math.random()*randomlinks.length)];
</script>
</head>
 
yes, like egood, looking for a sequential script ... 1 then 2, then 3 ...4, 5 .... then loops back to 1 ... in php would be great.
thnx guys!


Here is php one if you need it.

Code:
<?php
//Add as many links you want
$mylink[1] = '<a href="YOURLINK">YOUTEXT</a>';
$mylink[2] = '<a href="YOURLINK">YOURTEXT</a>';
$mylink[3] = '<a href="YOURLINK">YOURTEXT</a>';
//$mylink[4] = .....
//$mylink[5] = ....
// this will count your links itself and select a random one
$id = rand(1,count($mylink));
// this will display the random link
echo $mylink[$id];
?>
 
does anybody have a sequential link rotator script? i'm only finding ones with random link rotation!!
 
That would require SQL access or write access to the file system to store the previous link in the database...

I assume you have one or the other, right?

dN
 
Let me rephrase that. I'm looking for a url rotator script. example....If a person types in or clicks whatever.com they'll be directed to a different website each time in sequential order. I need all my sites to get an equal number of clicks
 
SQL is not needed you can use one of the following:

Code:
<?php
$linksfile ="urlrotator.txt";
 
$urls = file("$linksfile");
$urls=str_replace('\n','',$urls);
$goto=array_shift($urls);
array_push($urls,$goto);
$strurls=implode('\n',$urls);
 
$handle=fopen($linksfile,"w"); 
 
fwrite($handle,$strurls); 
 
fclose($handle);
 
header("Location: $goto");
 
?>

There are two issues here, a. performance hit of doing a large amount of disk writes and b. risk of getting your data messed up by two people writing to it at the same time. Two sollutions are needed. To avoid the excess disk writes you could have a seperate file with a number in, each time the page is hit this number is incremented untill it reaches the number of links and then goes back to 0. To avoid the possiblity of data ****up you can use php's file locking flock. Here's the modded code.

Code:
<?php
$linksfile ="urlrotator.txt";
$posfile = "pos.txt";
 
$links = file($linksfile);
$numlinks = count($linksfile);
 
$fp = fopen($posfile, 'r+') or die("Failed to open posfile");
flock($fp, LOCK_EX);
$num = fread($fp, 1024);
if($num<$numlinks-1) {
fwrite($fp, $num+1);
} else {
fwrite($fp, 0);
}
flock($fp, LOCK_UN);
fclose($fp);
 
header("Location: {$links[$num]}");
?>

You will need to create the urlrotator.txt and postxt file and upload it to the directory the php file is located. Also make sure the folder it is in, is writeable, just change the permissions in your FTP Program. Hope this helps.
 
Last edited:
thanks civic,but i'm a newbie when it comes to this coding stuff! Is there a way that you can modify this script you posted to be sequential? i was just looking for something i can paste on a blogger or wordpress blog.

thanks


Not sure if this is what you mean but here it is, it will load a random page upon opening the page that is code is in.

Code:
<head>
<script type="text/javascript" language="javascript">
var randomlinks=new Array("[URL]http://DOMAIN.com[/URL]", "[URL]http://DOMAIN.com[/URL]", "[URL]http://DOMAIN.com[/URL]", "[URL]http://DOMAIN.com[/URL]", "[URL]http://www.DOMAIN.com[/URL]");
location.href = randomlinks[Math.floor(Math.random()*randomlinks.length)];
</script>
</head>
 
The php is basically a copy and paste....just make the rotatorurl.txt and upload then use the php code i gave ya...and thats it.
 
Back
Top