Q:

extracting ID from youtube URL to embed video's

I'm trying to extract the video ID from youtube URLs to embed submitted videos to my site.
This way people can just submit the plain URL instead of the embed code.
$(document).ready(function () {
                                $('.video-field').html(function(i,v){
                                    var id = v.split('watch?v=')[1]; // get the id so you can add to iframe
                                    var be = v.split('youtu.be/')[2];
                                    return '<div class="fluid-width-video-wrapper" style="padding-top: 56.2%;"><iframe width="560" height="315" src="http://www.youtube.com/embed/' + id + '" frameborder="0" allowfullscreen></iframe></div>';

                                    return '<div class="fluid-width-video-wrapper" style="padding-top: 56.2%;"><iframe width="560" height="315" src="http://www.youtube.com/embed/' + be + '" frameborder="0" allowfullscreen></iframe></div>';

});                                         
                                }); 

Youtube URL's come in two flavours though, watch?v= + videoID and youtu.be/ + videoID.
Both have the same ID's, just different links depending on whether you copy from the browser bar or the share tab on youtube. 
The second one is returning 'undefined' in the output for me. 

A:

i believe both indexes should be [1]. replace the 'be' var with 
var be = v.split('youtu.be/')[1];

and you should be good.
Also should probably add an if to eval the 'v' argument, like this:
$(document).ready(function () {
    $('.video-field').html(function(i,v){
        var id;
        if(v.indexOf('watch?v=') > 0){
            id = v.split('watch?v=')[1];
        } else if(v.indexOf('youtu.be/') > 0) {
            id = v.split('youtu.be/')[1];
        }
        return '<div class="fluid-width-video-wrapper" style="padding-top: 56.2%;"><iframe width="560" height="315" src="http://www.youtube.com/embed/' + id + '" frameborder="0" allowfullscreen></iframe></div>';
    });                                         
});

