Get the YouTube video ID from YouTube video URL Using Javascript

Use the below function to get the YouTube video ID from YouTube video URL

/**
* @param video_url String YouTube video link
* @return String YouTube video ID
*/

function getMyYoutubeVideoId(video_url){
var l = document.createElement("a");
l.href = video_url;
var frameId='';
var hostname=l.hostname;
var path= l.pathname;
var query=l.search;
if(hostname=='youtu.be' || hostname=='www.youtu.be'){
frameId=path.replace('/','');
}else if(path=='/watch'){
var queryArray=query.split('=');
frameId=queryArray[1];
}else if(path.substr(0,7)=='/embed/'){
frameId=path.substr(7);
}
return frameId;
}

Comments

Popular posts from this blog

How To Use Crosswalk in Ionic 2 or 3 To Solve Compatibility Issue in Android

Create a Background Service Using systemd In Linux

HTACCESS FOR URL ROUTING FOR ANGULAR 2 APPLICATION