bratsun
6/6/2014 - 4:40 PM

Parse youtube ID from URL

Parse youtube ID from URL

function getVideoId(url){
  if(url.indexOf('?') != -1 ) {
    var query = decodeURI(url).split('?')[1];
    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;
}