Breaking News
Home / Web / Javascript / How to get video id from a Youtube url in Javascript?

How to get video id from a Youtube url in Javascript?

Snippet

e.g. if the youtube, video url is:

http://www.youtube.com/watch?v=xptDCPlSSfI&

The snippet will return xptDCPlSSfI

var url = “http://www.youtube.com/watch?v=xptDCPlSSfI”;


var video_id = url.split(‘v=’)[1];

var ampersandPosition = video_id.indexOf(‘&’);

if(ampersandPosition != -1)
{

video_id = video_id.substring(0, ampersandPosition);


}

[pb_builder]

Check Also

How to redirect page to another url using Javascript?

Sometimes we have to redirect a page to another location. This can be achieved by …

Advertisment ad adsense adlogger