随机HTML页面重定向

时间:2016-11-07 00:13:13

标签: javascript html redirect

所以我要做的就是拥有它,所以当你在我的一个网页上时,很有可能被随机重定向到另一个页面。像五分之一的机会。这甚至可能吗?请帮忙。

4 个答案:

答案 0 :(得分:1)

是的,如果您执行以下操作:

var redirect = [/*500 different pages with "" around each and each separated by , outside ""*/]

window.location.href = redirect[Math.floor(Math.random() * 500)]

答案 1 :(得分:1)

这可以在javascript中轻松完成。

只需获取0到500之间的随机数,让我们说如果随机数是0,那么我们会将用户重定向到另一个页面,否则我们不会。

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

    if (randNum == 0)
        window.open('redirect-url', '_self');
</script>

如果您想将用户从网站列表重定向到随机网站,您可以执行以下操作:

<script>
    // get a random number between 0 and 499
    //
    var randNum = Math.floor(Math.random() * 500);

    // An array of URL's
    var randURLs = [
        "http://url0",
        "http://url1",
        "http://url1"
    ];

    // There was a 1 in 500 chance we generated a zero.
    if (randNum == 0) {
        // randURLs.length will tell us how many elements are
        // in the randURLs array - we can use this to generate
        // a random number between 0 and n (number of elements)
        //
        // In our case there are 3 elements in the array, 0, 1
        // and 2. So we want to get another random number in
        // the inclusive range 0 - 2
        //
        var randURL = Math.floor(Math.random() * randURLs.length);

        window.open(randURLs[randURL]);
    }
</script>

答案 2 :(得分:0)

是。你可以使用这个功能,它有可能是500分之一:

<?php 
    if (rand(0,500)==250) 
         random_redirect();
    random_redirect(){
    header("Location: http://www.yourwebsite.com/user.php"); /* Redirect browser */}
?>

或者在javascript中:

var rand = Math.floor((Math.random()*500)+1);
if ( rand==250){
    window.location = "http://www.yoururl.com";}

答案 3 :(得分:0)

以下是我在评论中的意思:

var test = Math.floor(Math.random() * 500) <= 1;

if (test) {
    window.location = "http://www.yoururl.com";
}