簡體   English   中英

隨機HTML頁面重定向

[英]Random HTML page redirect

所以我要做的就是擁有它,所以當你在我的一個網頁上時,很有可能被隨機重定向到另一個頁面。 像五分之一的機會。 這甚至可能嗎? 請幫忙。

這可以在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>

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

 var redirect = [/*500 different pages with "" around each and each separated by , outside ""*/] window.location.href = redirect[Math.floor(Math.random() * 500)] 

是。 你可以使用這個功能,它有可能是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";}

這是我在評論中的意思:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM