js使用iframe引入youtube视频到页面中播放,解决跨域播放

首先肯定要能翻墙了,才能播放youtube视频

直接上代码

这里的主要原理是使用的embed-container,这是一个html的 class

实际标签是这样的,iframe中引入的youtube视频链接,不建议写这么复杂,看下面

<div class="edy-texteditor-container embed-container edy-positionable-container-maxwidth" contenteditable="false" unselectable="on" style="max-width: 100%; overflow: hidden; position: relative; width: auto;">

<div class="edy-padding-resizer-wrapper" style="padding-bottom: 56.25%;">

<iframe id="ifrId" src="//www.youtube.com/embed/v4NYCh_nBYU?wmode=transparent" frameborder="0" style="position: absolute; display: block; width: 100%; height: 100%;"></iframe>

</div>

</div>


但是其实只要把链接修改一下就好了,下面是我动态js创建的iframe,然后把youtube

视频链接转换一下,放到iframe的src上,然后再添加到div中就可以加载出youtube视频(需要翻墙了),可以点击观看

url 就是带有https/http链接的,这里是https,例如url= 'https://www.youtube.com/watch?v=pqkMbz2MIKA'

 

let tmpUrl  = url.replace('https:','')  // 去掉https:

// watch?转化成embed/,添加wmode,这个wmode可以自己去学习看看

  tmpUrl = tmpUrl.replace('watch?v=','embed/')+'?wmode=transparent' 

// 然后创建iframe,使用哪个tmpUrl就可以了
  var exec_obj = document.createElement('iframe'); 
exec_obj.name = 'tmp_frame'; 
exec_obj.src = tmpUrl ; //可以访问controller携带参数
//exec_obj.style.display = 'none';
document.body.appendChild(exec_obj);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章