extract and replace (YouTube ONLY) Iframe src

我是研究僧i 提交于 2019-12-23 04:24:17

问题


If a YouTube iframe html is

<iframe width="560" height="315" src="http://www.youtube.com/embed/VIDEOID" frameborder="0" allowfullscreen></iframe>

The jquery should test the iframe src to see if the first 29 characters match http://www.youtube.com/embed/

if it does apply a jquery to extract the VIDEOID after /embed/ and replace the iframe src with http://www.redirect.mysite.com/?id=VIDEOID

so the iframe's src below

<iframe width="560" height="315" src="http://www.youtube.com/embed/EhrYQrLBiTQ" frameborder="0" allowfullscreen></iframe>

should be replace with

<iframe width="560" height="315" src="http://www.redirect.mysite.com/?id=EhrYQrLBiTQ" frameborder="0" allowfullscreen></iframe>

回答1:


See this fiddle: http://jsfiddle.net/maniator/cRFgv/

Code:

$('iframe').each(function(){
    this.src = this.src.replace('http://www.youtube.com/embed/', 
                                         'http://www.redirect.mysite.com/?id=');
});


来源:https://stackoverflow.com/questions/8041352/extract-and-replace-youtube-only-iframe-src

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!