The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> ramdon load to different pages
anrick
post Mar 6 2007, 08:26 PM
Post #1





Group: Members
Posts: 5
Joined: 20-October 06
Member No.: 502



Hello people.

My site has an index-page which automatically directs traffic to one of four different pages randomly.
I'm using a bit of javascript code but I'm not really happy with it. Its not very random (tends to favour the first page massively) and I was wondering if anyone out there has a better script for this purpose.

Below what I am using.
Any help would be brilliant.

Regards,
A.

<script LANGUAGE="JavaScript">

<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
var howMany = 3; // max number of items listed below
var page = new Array(howMany+1);

page[0]="page00.html";
page[1]="page01.html";
page[2]="page02.html";
page[3]="page03.html";
function rndnumber(){
var randscript = -1;
while (randscript < 0 || randscript > howMany || isNaN(randscript)){
randscript = parseInt(Math.random()*(howMany+1));
}
return randscript;
}
quo = rndnumber();
quox = page[quo];
window.location=(quox);
// End -->
</SCRIPT>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Mar 6 2007, 10:28 PM
Post #2


Jocular coder
********

Group: Members
Posts: 2,460
Joined: 31-August 06
Member No.: 43



QUOTE
var randscript = -1;
while (randscript < 0 || randscript > howMany || isNaN(randscript)){
randscript = parseInt(Math.random()*(howMany+1));
}
return randscript;
}


Where did you get this? Seems very odd - parseInt is supposed to convert a string to an integer, but Math.random returns a float. Given the intellectually promiscuous notion of typing in javascript, who knows what the result would be.

I think you want something like Math.floor(Math.random() * reallyHowMany)

where reallyHowMany = 4 in the above example. (Currently howMany is not how many, but the index of the last element in a 0-indexed array.)

*** You put this in Server-side scripting - why? Actually it would surely be a better idea to do it on the server, using PHP or whatever you have available?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
anrick
post Mar 7 2007, 06:50 AM
Post #3





Group: Members
Posts: 5
Joined: 20-October 06
Member No.: 502



I'm not really familiar enough with PHP to be able to figure it out. My web-design skills are pretty basic - I just did a web-search for "random load javascript" and this code is one which I managed to find.

If you have an easy but better solution that does the same thing I'd love to hear it.

With reference to your suggestion above, my code should look like this right?

var randscript = -1;
while (randscript < 0 || randscript > howMany || isNaN(randscript)){
randscript = Math.floor(Math.random() * reallyHowMany);
}
return randscript;
}
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Mar 7 2007, 11:04 AM
Post #4


Jocular coder
********

Group: Members
Posts: 2,460
Joined: 31-August 06
Member No.: 43



QUOTE(anrick @ Mar 7 2007, 08:50 PM) *

I'm not really familiar enough with PHP to be able to figure it out. My web-design skills are pretty basic - I just did a web-search for "random load javascript" and this code is one which I managed to find.

If you have an easy but better solution that does the same thing I'd love to hear it.

With reference to your suggestion above, my code should look like this right?

var randscript = -1;
while (randscript < 0 || randscript > howMany || isNaN(randscript)){
randscript = Math.floor(Math.random() * reallyHowMany);
}
return randscript;
}


Not exactly. The point is that the original script is screwy: of course in any programming language you can write something like this:

var three = 4;
var one = 7;

var ans = one + three;
print ans;

... and if everything is as expected the correct result is 11. But you have to be a pervert of some sort to enjoy doing it. Your programmer has a variable called howMany, but the value of howMany is not how many, but how many minus one. This makes it Hard to see what's going on.

Actually this is classic suck-it-and-see programming. He has a function supposed to return a random number in the range 0 to howMany inclusive (where howMany is not how many, ok?). Obviously the first version of this function was so wrong it returned all sorts of wrong values, so he sticks in a test: "Just keep doing it until we get a correct answer".

Here's a different program, copyright me, and you may use it quite happily as long as you do not imply any association with the original lot.

var howMany = 4; // number of items listed below
var page = new Array(howMany);

page[0]="page00.html";
page[1]="page01.html";
page[2]="page02.html";
page[3]="page03.html";

var pageno = Math.floor(Math.random() * howMany) % howMany; // might just be 4.0!

window.location=(page[quo]);

*** I have not tested this ***

I still think it would be better to do this on the server [!!! HELLO !!! Why did someone move this ???]
and my PHP version is more likely to work first time. You also have the choice of sending redirects, so that the user sees a different address for each of the pages, or just sending the selected page _as_ the original address. Which is better depends on what you are doing.

<?php
$pages = array('first.html', 'second.html', 'third.html', 'fourth.html');
$goto = rand(0, count($pages)-1);

// EITHER - to redirect to new page
header("Location: http://<<<WHATEVER YOUR URL IS>>>/$goto");

// OR - if pages are in the same directory
include($goto);

// END
?>





User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
anrick
post Mar 9 2007, 08:28 AM
Post #5





Group: Members
Posts: 5
Joined: 20-October 06
Member No.: 502



Look - I'm feeling really stupid for having to get back to you. Many thanks for all the help. I'd like to try to get your PHP script working as ideally I'd keep the same url visible in the browser while users are directed to different pages.

I've added this to my html but of course, I'm doing something wrong.
Something tells me php is not like javascript -
Can you suggest, advise, correct?

Again - many thanks.
A.

<?php
$pages = array('hello_cans.html', 'hello_eating.html', 'hello_kabuki.html', 'hello_scientist.html');
$goto = rand(0, count($pages)-1);

include($goto);

// END
?>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Mar 9 2007, 12:37 PM
Post #6


Jocular coder
********

Group: Members
Posts: 2,460
Joined: 31-August 06
Member No.: 43



You do need to have php running on your server, and you need to make sure the server is set up to parse the page as php. If it's called something.php this probably happens automagically, but if not, um, see the FAQ or something, because how to set up Apache to parse .html files with php is a standard (frequent) question.

(I'm assuming that a browser just shows the text of the php program, right?)
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Mar 9 2007, 04:23 PM
Post #7


.
********

Group: WDG Moderators
Posts: 9,661
Joined: 10-August 06
Member No.: 7



edit: misunderstanding blink.gif
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
anrick
post Mar 10 2007, 09:00 PM
Post #8





Group: Members
Posts: 5
Joined: 20-October 06
Member No.: 502



Brian - I have absolutely no idea what youre talking about. Well, I'm lying - I get the basic idea but I'll try to do what I need to do with javascript for now. One day I'm going to read-up on PHP a little more. Something I've been meaning to do for ages. But for now its a little beyond me.

Many thanks for the help - I'll see how far I get.
A.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
liliputland
post Sep 3 2008, 11:44 AM
Post #9





Group: Members
Posts: 1
Joined: 3-September 08
Member No.: 6,577



hello,

i'm trying to do the same thing but i can't get the html code posted to work. have you got it up and running? can't see what i'm doing wrong.

help!!
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 28th April 2024 - 11:47 AM