Last active
August 29, 2015 14:27
-
-
Save remoblaser/7146f1d2af40fe929c96 to your computer and use it in GitHub Desktop.
Parse Youtube / Vimeo Video URL to iFrame
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getCode() { | |
var pattern1 = /(?:http?s?:\/\/)?(?:www\.)?(?:vimeo\.com)\/?(.+)/g; | |
var pattern2 = /(?:http?s?:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch\?v=)?(.+)/g; | |
var regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/; | |
var video = "https://vimeo.com/58821159"; | |
if(pattern1.test(video)) { | |
var video_id = video.split('/'); | |
var id = video_id[video_id.length -1]; | |
return '<iframe width="100%" height="720" src="//player.vimeo.com/video/' + id + '" frameborder="0" webkitall owfullscreen mozallowfullscreen allowfullscreen></iframe>'; | |
} | |
if(pattern2.test(video)){ | |
var video_id = video.split('v=')[1]; | |
var ampersandPosition = video_id.indexOf('&'); | |
if(ampersandPosition != -1) { | |
video_id = video_id.substring(0, ampersandPosition); | |
} | |
return '<iframe width="100%" height="720" src="http://www.youtube.com/embed/' + video_id + ' frameborder="0" allowfullscreen></iframe>'; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment