Screeching Kitten Soul Screeching Kitten Soul - 1 year ago 102
Javascript Question

Random HTML page redirect

So what I'm trying to do is have it so when you are on one of my webpages, there is a small chance to be randomly redirected to another page. Like 1 in 500 chance. Is this even possible? Please help.

Answer Source

This could be done easily in javascript.

Just get a random number between 0 and 500, let's say if the random number is 0 then we redirect the user to another page, otherwise we don't.

    var randNum = Math.floor(Math.random() * 500);

    if (randNum == 0)'redirect-url');

If you want to redirect the user to a random website from a list of websites you can do something like this:

    var randNum = Math.floor(Math.random() * 500);

    var randURLs = [ "http://url0", "http://url1", "http://url1" ];

    if (randNum == 0) {
        var randURL = Math.floor(Math.random() * randURLs.length);[randURL], '_blank');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download