在window.onload

时间:2015-05-07 04:29:31

标签: javascript html

我有以下代码。这意味着要嵌入YouTube视频。然而,似乎它以某种方式不会改变它应该的div。

window.onload = embed;
function embed() {
    var apikey = "SNIP"; //Set to your API Key
    // See NOTEA below
    var channelName = "ThisWeekInScience"; //Set to your channel name
    var channelID = nameToID(channelName,apikey);
    //NOTEA: If you know it you can of course just comment that out and set the id manually.
    var video = checkLive(channelID,apikey);
    var autoplay = "1";
    if(video.length == 0) {
        video = archiveVideo(channelID,apikey);
        autoplay = "0";
    }
    var embedCode = "<object height=\"350\" width=\"425\"><param name=\"movie\" value=\"https://www.youtube.com/v/" + video + "&autoplay=" + autoplay + "\"><embed height=\"350\" width=\"425\" type=\"application/x-shockwave-flash\" src=\"https://www.youtube.com/v/" + video + "&autoplay=" + autoplay + "\"></embed></object>";
    document.getElementById("liveembed").innerHTML = embedCode;
}
function nameToID(channelName,apikey) {
    var getChannelID = new XMLHttpRequest();
    getChannelID.open("GET","https://www.googleapis.com/youtube/v3/channels?part=id&forUsername=" + channelName + "&fields=items%2Fid&key=" + apikey,false);
    getChannelID.send();
    var channelIDobj = JSON.parse(getChannelID.responseText);
    return channelIDobj.items[0].id;
}
function checkLive(channelID,apikey) {
    // In future check if soon live and refresh until live
    var getLive = new XMLHttpRequest();
    getLive.open("GET","https://www.googleapis.com/youtube/v3/search?part=id&channelId=" + channelID + "&eventType=live&maxResults=1&type=video&fields=items%2Fid&key=" + apikey,false);
    getLive.send();
    var liveVideo = JSON.parse(getLive.responseText);
    if(liveVideo.items[0] === undefined) {
        return "";
    }
    else {
        return liveVideo.items[0].id.videoId;
    }
}
function archiveVideo(channelID,apikey) {
    var getPlaylist = new XMLHttpRequest();
    getPlaylist.open("GET","https://www.googleapis.com/youtube/v3/channels?part=contentDetails&id=" + channelID + "&fields=items%2FcontentDetails&key=" + apikey,false);
    getPlaylist.send();
    var playlistObj = JSON.parse(getPlaylist.responseText);
    var playlistID = playlistObj.items[0].contentDetails.relatedPlaylists.uploads;
    var getArchiveVideo = new XMLHttpRequest();
    getArchiveVideo.open("GET","https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails&maxResults=1&playlistId=" + playlistID + "&fields=items%2FcontentDetails&key=" + apikey,false);
    getArchiveVideo.send();
    var archiveVideoObj = JSON.parse(getArchiveVideo.responseText);
    return archiveVideoObj.items[0].contentDetails.videoId;
}

由于我使用xhtml transitional在WordPress中使用的主题,我无法使用HTML5。我也想让它尽可能便携,因此不想使用jQuery或其他库。

它的当前版本位于http://www.twis.org/live/ 我还有一个运行在https://scienceisland.org/science-island-chat/

的版本

2 个答案:

答案 0 :(得分:0)

您链接的网页包含JavaScript错误。

jQuery("a.fancybox").fancybox({
    'cyclic': false,
    'autoScale': false,
    'padding': ,
    'opacity': false,

填充没有价值。删除该行,或根据fancybox文档给它一个值。

答案 1 :(得分:-1)

事实证明,在页面中有一个window.onload是个问题。