2011-06-02 28 views
0

我正在尋找類似於下面的代碼的東西,但它應該隨機選擇一個鏈接(或者它是否可以與iframed視頻一起使用?),以在側面刷新和/或模糊時加載。Javascript onload/onblur,隨機打開一個鏈接?

這怎麼可以隨機化?

<script> 
window.onblur = function() { 
    TIMER = setTimeout(changeItUp, 5000); 
} 

window.onfocus = function() { 
    if (TIMER) clearTimeout(TIMER); 
} 

function changeItUp() { 
    location.href = "http://www.yahoo.com" 
} 
</script> 

回答

0
//return a random number between 0 and 1 
document.write(Math.random() + "<br />"); 

//return a random integer between 0 and 10 
document.write(Math.floor(Math.random()*11)); 

然後,只需與你聯繫的數組,並選擇一個隨機

linkArray[Math.floor(Math.random()*11)] 

很基本的東西,但是,你應該谷歌這樣的事情。