millsi80
06-18-2008, 02:32 PM
Hi,
I am pretty much a noob when it comes to javascript. I am looking for a script that redirects random.html to 1 of 86 predefined pages, randomly. If someone could help me out, that would be great.
Thanks for your help
Philip M
06-18-2008, 02:42 PM
<script type = "text/javascript">
function reDirect() {
x = Math.ceil(Math.random() * 86);
newPage = "Page" + x + ".html";
window.location = newPage;
}
</script>
All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
millsi80
06-18-2008, 03:53 PM
Thanks for the quick response! I just have 2 quick questions. Do I put this code in body or in the head? And, do I need edit this or just copy paste?
Thanks
Philip M
06-19-2008, 09:07 AM
Thanks for the quick response! I just have 2 quick questions. Do I put this code in body or in the head? And, do I need edit this or just copy paste?
Thanks
In the <head>.
Well, obviously you must edit the page names to your actual names.
newPage = "Page" + x + ".html";
This will redirect to Page45.html etc.
I have assumed your pages are named Page1.html through Page86.html.
millsi80
06-19-2008, 02:06 PM
So I put the code in the head part but it doesn't work. I modified it to test with 2 pages
<head>
<script type = "text/javascript">
function reDirect() {
x = Math.ceil(Math.random() * 2);
newPage = "Page" + 1 + ".html";
newPage = "Page" + 2 + ".html";
window.location = newPage;
}
</script>
</head>
When I go to random.html, it doesn't redirect
Please tell me what I did wrong
Philip M
06-19-2008, 02:12 PM
Just follow what you have been given:-
newPage = "Page" + x + ".html";
You need to call the function:-
<script type = "text/javascript">
function reDirect() {
x = Math.ceil(Math.random() * 86); // change 86 to 2 for your initial test
newPage = "Page" + x + ".html";
window.location = newPage;
}
reDirect();
</script>
millsi80
06-19-2008, 02:26 PM
Ok, thanks very much, works perfect!
drodax
10-14-2008, 09:53 PM
Hi,
This works great..but is there a way to make it so that there is a limit to how many times the random page has been reloaded?
Let say for example once it randomly redirects to these pages.x.html, y.html, z.html can you give it a limit to how many times the each page has been reloaded? let say each page has been reloaded 5 times already and after that.. either have it timeout or redirect to another page? It sounds simple, but I have limited knowledge with javascript, and cant figure out how to do this. or maybe this needs to be Server-side?
any help would do..Thanks!
akshay_rautela
09-15-2010, 03:54 PM
@philip m
Thnx dude,worked like a charm for me. :D