Javascript 正在获取给定id的Youtube标题
我找到了一个很好的参考,可以在给定youtube视频id的情况下获取youtube视频标题,但我不确定如何在他提供的函数中返回标题。在videoInfoCallback函数中,我有返回消息的功能,但我不确定传递给info参数的是什么。任何建议。以下是我正在使用的资源: 下面是我正在使用的代码:Javascript 正在获取给定id的Youtube标题,javascript,jquery,html,json,youtube,Javascript,Jquery,Html,Json,Youtube,我找到了一个很好的参考,可以在给定youtube视频id的情况下获取youtube视频标题,但我不确定如何在他提供的函数中返回标题。在videoInfoCallback函数中,我有返回消息的功能,但我不确定传递给info参数的是什么。任何建议。以下是我正在使用的资源: 下面是我正在使用的代码: function registerScript(url) { var s = document.createElement('script'); s.typ
function registerScript(url) {
var s = document.createElement('script');
s.type = 'text/javascript';
s.src = url;
document.getElementsByTagName('head')[0].appendChild(s);
}
function videoInfoCallback(info) {
if (info.error) {
alert('Error\n\n' + info.error.message);
} else {
var message = info.data.title;
return message;
}
return "hello";
}
function getVideoInformation(id) {
if (id) {
return registerScript('https://gdata.youtube.com/feeds/api/videos/' + id + '?v=2&alt=jsonc&callback=videoInfoCallback');
} else {
alert('Please enter an id.');
}
}
基本上,我希望有一个函数,可以接收像这样的CFF0mV24WCY的Youtube视频id,并让它返回标题。这并不是真的回答您上面的问题,但它可能会帮助您完成任务 它只是一个简单的youtube搜索,带来了标题视频和浏览量 html
JavaScript
$(document).ready(function () {
$("#show").click(function () {
getYoutube($("#Search").val() + "Offical Film Trailer");
});
});
function getYoutube(title) {
$.ajax({
type: "GET",
url: yt_url = 'http://gdata.youtube.com/feeds/api/videos?q=' + title + '&format=5&max-results=1&v=2&alt=jsonc',
dataType: "jsonp",
success: function (response) {
if (response.data.items) {
$.each(response.data.items, function (i, data) {
var video_id = data.id;
var video_title = data.title;
var video_viewCount = data.viewCount;
var video_frame = "<iframe width='600' height='385' src='http://www.youtube.com/embed/" + video_id + "' frameborder='0' type='text/html'></iframe>";
var final_res = "<div id='title'>" + video_title + "</div><div>" + video_frame + "</div><div id='count'>" + video_viewCount + " Views</div>";
$("#result").html(final_res);
});
} else {
$("#result").html("<div id='no'>No Video</div>");
}
}
});
}
$(文档).ready(函数(){
$(“#显示”)。单击(函数(){
getYoutube($(“#搜索”).val()+“官方电影预告片”);
});
});
函数getYoutube(标题){
$.ajax({
键入:“获取”,
url:yt_url='1http://gdata.youtube.com/feeds/api/videos?q=“+title+”&format=5&max results=1&v=2&alt=jsonc',
数据类型:“jsonp”,
成功:功能(响应){
if(response.data.items){
$。每个(response.data.items,函数(i,数据){
var video_id=data.id;
var video_title=data.title;
var video_viewCount=data.viewCount;
var video_frame=“”;
var final_res=“”+视频标题+“”+视频帧+“”+视频视图计数+“视图”;
$(“#结果”).html(最终结果);
});
}否则{
$(“#结果”).html(“无视频”);
}
}
});
}
您可以在浏览器中使用调试器,并在回调函数中设置断点,以查看info
包含的内容。好的,我知道内容是什么,但它只是一个包含所有视频信息的对象。这是在做什么?v=2&alt=jsonc&callback=videoInfoCallback'?它们是YouTube API的查询参数v
是API的版本,alt
是返回数据的格式,callback
是返回数据后启动的回调函数。谢谢!我会检查它,告诉我它是否有用final
是保留关键字
不要将其用作变量名。我已将其更改为final\u res
。
$(document).ready(function () {
$("#show").click(function () {
getYoutube($("#Search").val() + "Offical Film Trailer");
});
});
function getYoutube(title) {
$.ajax({
type: "GET",
url: yt_url = 'http://gdata.youtube.com/feeds/api/videos?q=' + title + '&format=5&max-results=1&v=2&alt=jsonc',
dataType: "jsonp",
success: function (response) {
if (response.data.items) {
$.each(response.data.items, function (i, data) {
var video_id = data.id;
var video_title = data.title;
var video_viewCount = data.viewCount;
var video_frame = "<iframe width='600' height='385' src='http://www.youtube.com/embed/" + video_id + "' frameborder='0' type='text/html'></iframe>";
var final_res = "<div id='title'>" + video_title + "</div><div>" + video_frame + "</div><div id='count'>" + video_viewCount + " Views</div>";
$("#result").html(final_res);
});
} else {
$("#result").html("<div id='no'>No Video</div>");
}
}
});
}