js随机动态创建多个iframe

<script>
function getRandomArrayElements(arr, count) { //数组随机取值
    var shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index;
    while (i-- > min) {
        index = Math.floor((i + 1) * Math.random());
        temp = shuffled[index];
        shuffled[index] = shuffled[i];
        shuffled[i] = temp;
    }
    return shuffled.slice(min);
}


var items = [
"http://www.taobao.com",
"http://www.taobao.com",
"http://www.taobao.com",
"http://www.taobao.com",
"http://www.taobao.com",
"http://www.baidu.com"
];




window.onload= function()
    {        
        var opres= getRandomArrayElements(items, 4) ;
        var i =1;
        for (x in opres)  //循环创建iframe
        {
            //document.write(opres[x] + "<br />");    
            var iframe = document.createElement("iframe");
                    iframe.src=opres[x] ;
                    iframe.className="frame";
                    iframe.id="iframe"+i;
                    iframe.width ="800px";
                    iframe.height ="400px";
                    document.body.appendChild(iframe);
                i++;
        }

    }

setTimeout("self.location.reload();",15000); //定时刷新
</script>


暂无留言,赶快评论吧

欢迎留言