PS:视图呈现HTML和javascript.
HTML<div ID="player"></div>JavaScript:var tag = document.createElement('script'); tag.src = "https://www.youtube.com/iframe_API"; var firstScriptTag = document.getElementsByTagname('script')[0]; firstScriptTag.parentNode.insertBefore(tag,firstScriptTag); var player; function onYouTubeiframeAPIReady() { player = new YT.Player('player',{ height: '486',wIDth: '864',vIDeoID: '#{@media['youtube_url']}',events: { 'onReady': onPlayerReady,'onStateChange': onPlayerStateChange },playerVars: { 'showinfo': 0,'iv_load_policy': 3,'color': 'white','fs': 1,'autoplay': 1,'vq': 'hd720' } }); } function onPlayerReady(event) { event.target.playVIDeo(); } var done = false; function onPlayerStateChange(event) { if (event.data == YT.PlayerState.PLAYING && !done) { setTimeout(stopVIDeo,6000); done = true; } } function stopVIDeo() { player.stopVIDeo(); }解决方法 只需检查脚本是否已定义.
if(document.getElementByID('iframe_API') === null){ var tag = document.createElement('script'); tag.src = "https://www.youtube.com/iframe_API"; tag.ID = "iframe_API"; var firstScriptTag = document.getElementsByTagname('script')[0]; firstScriptTag.parentNode.insertBefore(tag,firstScriptTag); }else runPlayer();function runPlayer(){ var player; player = new YT.Player(element.children()[0],{ playerVars: { autoplay: 1,HTML5: 1,controls: 1,showsearch: 0,showinfo: 0 },height: scope.height,wIDth: scope.wIDth,vIDeoID: scope.vIDeoID,events: { onStateChange: function (event) { if (event.data == YT.PlayerState.ENDED) { scope.$emit('VIDeoEnded'); } } } }); }$window.onYouTubeiframeAPIReady = function () { runPlayer();};总结
以上是内存溢出为你收集整理的Youtube iFrame(使用javascript API)第一次加载,但不加载第二次.为什么?全部内容,希望文章能够帮你解决Youtube iFrame(使用javascript API)第一次加载,但不加载第二次.为什么?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)