javascript - Youtube URL to Iframe -
i running ticketing software ability manipulate css , js customization , need convert video url iframe. using code:
$('#wiki-page-content').contents().each(function() { // skip non text nodes. if (this.nodetype !== 3) { return true; } // grab text var matches = this.data.match(/(?:http:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch\?v=)?(.+)/g); if (!matches) { return true; } var iframe = $('<iframe width="420" height="345" frameborder="0" allowfullscreen />', { src: 'http://www.youtube.com/embed/' + matches[1] }); iframe.insertafter(this); $(this).remove();});
now, problem when puts url inside wiki page using text editor, wraps <p></p>
tag around it. causing problem? how can on this.
the tag has notetype equals 1. try this:
$('#wiki-page-content').contents().each(function() { // skip non text nodes. var text; switch (this.nodetype) { case 3: text = this.data; break; case 1: text = this.innerhtml; // or this.textcontent break; default: return true; } // grab text var matches = text.match(/(?:http:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch\?v=)?(.+)/g); if (!matches) { return true; } var iframe = $('<iframe width="420" height="345" frameborder="0" allowfullscreen />', { src: 'http://www.youtube.com/embed/' + matches[1] }); iframe.insertafter(this); $(this).remove(); });
Comments
Post a Comment