Skip to content

Instantly share code, notes, and snippets.

@bratsun
Created November 11, 2014 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bratsun/364f63459a772123d297 to your computer and use it in GitHub Desktop.
Save bratsun/364f63459a772123d297 to your computer and use it in GitHub Desktop.
Turn Vimeo or YouTube URL into embedded video
// vimeo links
$('a[href*="vimeo.com"]').each(function() {
$this = $(this);
var href = '//player.vimeo.com/video/' + $this.attr('href').split('/').pop();
$this.before('<div class="video-embed"><iframe src="//player.vimeo.com/video/'+$this.attr('href').split('/').pop()+'?autoplay=0&amp;title=0&amp;byline=0&amp;color=1A9691&amp;portrait=0&amp;hd_off=0" width="600" height="337.5" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></div>');
});
// youtube links
function getVideoId(url){
if(url.indexOf('?') != -1 ) {
var query = decodeURI(url).split('?')[1];
//console.log(query);
var params = query.split('&');
for(var i=0,l = params.length;i<l;i++)
if(params[i].indexOf('v=') === 0)
return params[i].replace('v=','');
} else if (url.indexOf('youtu.be') != -1) {
return decodeURI(url).split('youtu.be/')[1];
}
return null;
}
$('a[href*="youtube.com"]').each(function() {
var t = $(this);
var yid = getVideoId(t.attr('href'));
t.before('<div class="video-embed"><iframe src="http://www.youtube.com/embed/' + yid +'?rel=0&autoplay=0" width="600" height="337.5" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></div>');
});
.video-embed {
height: 0;
padding-bottom: 50.5% !important;
padding-top: 25px;
position: relative;
width: 100% !important;
}
.video-embed iframe {
position: absolute;
top: 0;
width: 100% !important;
height: 100% !important;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment