嵌入代码中的随机链接

Random links in embed code

本文关键字:随机 链接 代码      更新时间:2023-09-26

我有一个小的视频嵌入代码。

<iframe width="640" height="360" scrolling="no" src="http://link" frameborder="0" allowfullscreen></iframe>

我需要创建一个按钮,它将随机选择从数组的链接,并使其作为一个"src"在上面的代码。

基本上是一个随机的视频生成器。

谢谢

您可以在iframe上放置一个ID,而不设置任何SRC(或默认有一个)。并有一个按钮来填充它。

<button onclick="randomVideo()">Random video</button>
<iframe src="" id="video" allowfullscreen></iframe>

然后你有你的链接数组,和一个函数,将选择一个随机的

JS

var links = [ "link1", "link2", "link3" ];
var random = 0;
// If you want a random video to be chosen on page load
randomVideo();
function randomVideo() {
    var temp = random;
    // Make sure we do not display the same video twice in a row
    while(temp == random) temp = Math.floor(Math.random() * links.length);
    random = temp;
    document.getElementById("video").src = links[random];
}

JS Fiddle Demo