Monday, July 11, 2011

Youtube video thumbnail image using javascript

Javascript function to get video thumbnail image from youtube

The code

function getScreen( url, size )
{
if(url === null){ return ""; }

size = (size === null) ? "big" : size;
var vid;
var results;

results = url.match("[\\?&]v=([^&#]*)");

vid = ( results === null ) ? url : results[1];

if(size == "small"){
return "http://img.youtube.com/vi/"+vid+"/2.jpg";
}else {
return "http://img.youtube.com/vi/"+vid+"/0.jpg";
}
}


Calling the javascript function and alerting the result

var result = '';

result = getScreen( 'http://www.youtube.com/watch?v=4OoC8ycuCo0&feature=topvideos_sports', 'small' );

alert(result);


The php version of the above code can be found in this blog

No comments:

Post a Comment