2016-01-13 117 views
0

我想通過使用javascript實現url點擊鏈接隨機方法。javascript循環鏈接點擊

例如,如果訪客A,B,C,d訪問我的網站

,如果點擊URL鏈接,我想遊客參觀我的兩個網站example1.com和example2.com隨機。

<area shape="rect" coords="140,350,890,440" href="http://somesite.com" target="_blank"> 

我發現了一些JavaScript的數組方法,但我怎麼能修改它,可以採用我的情況?

<SCRIPT Language="Javascript"> 
<!-- 
function Randomlink() 
{ 
    Url = new Array; 
    Url[0] = "http://www.ccm.net/index.php3"; 
    Url[1] = "http://www. ccm.net/wifi"; 
    Url[2] = "http://www. ccm.net/php"; 
    Url[3] = "http://www. ccm.net/forum/"; 

    Chooselink = Math.round(Math.random() * (Url.length+1)); 
    window.open(Url[Chooselink],'_blank'); 
} 
//--> 
</SCRIPT> 
+0

在這個算法中使用Math.round()並不能給你均勻的分佈。看例子:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random – cgatian

回答

1

爲什麼你需要JavaScript爲此?如果您使用的是PHP,可以在那裏完成。

$sitenum = rand(1,2); 

print <<<END 
<area shape="rect" coords="140,350,890,440" href="http://somesite$sitenum.com" target="_blank"> 
END; 
+0

我的簡單網站只包含html文件而不是php。所以可能的解決方案是使用JavaScript。謝謝 – softfx

+0

好的。你用php標記了你的問題,所以我認爲你也在使用它。 – weevil