对于HTML中的iframe循环,请点按“播放”按钮

时间:2019-01-16 02:33:44

标签: javascript html arrays for-loop

我正在尝试创建一个for循环,以显示放置在特定列表中的每个URI的Spotify播放按钮。因此,如果这张专辑spotify:album:08Cq2FeFxRQdpkKrjCGrth在列表中,它将显示为

<iframe src="https://open.spotify.com/embed/album/08Cq2FeFxRQdpkKrjCGrth" width="300" height="380" frameborder="0" allowtransparency="true" allow="encrypted-media"></iframe>

因此for循环会将URI放入iframe标记中。

这是我拥有的以下代码,我已经在线查看了许多教程和示例,但无法解决这种情况。

var list = ["spotify:album:08Cq2FeFxRQdpkKrjCGrth"];
for (var i = 0; i < list.length; i++) {
    var source = list[i].split(":")[2];
    var iframe = document.createElement("iframe");
    iframe.src = source;
    iframe.width = "300";
    iframe.height = "380";
    iframe.frameborder = "0";
    iframe.allowtransparency = "true";
    iframe.allow = "encrypted-media";
    document.getElementById("demo").appendChild(iframe);
}

1 个答案:

答案 0 :(得分:0)

这是我想到的。但是您需要将其部署到服务器上才能工作,否则即使您添加了沙箱,也无论如何都会出现此错误。需要将其部署到具有正确的http / https url的服务器上(本地或ipaddress url均无效)

embed.2019-01-15_bcdc2b81.js:1 Uncaught DOMException: Failed to read the 'cookie' property from 'Document': The document is sandboxed and lacks the  

var list = ["spotify:album:1DFixLWuPkv3KT3TnV35m3"];
var url = "https://embed.spotify.com?uri="
for (var i = 0; i < list.length; i++) {
    var source =url + list[i]
    var iframe = document.createElement("iframe");
    iframe.src = source;
    iframe.width = "300";
    iframe.height = "380";
    iframe.frameborder = "0";
    iframe.allowtransparency = "true";
    iframe.allow = "encrypted-media";
    document.getElementById("demo").appendChild(iframe);
    
}
iframe{
border:1px solid red;
width:100%;
height:100%;
}
<div id="demo"></div>