在外部js文件中调用时未定义视频Js

时间:2018-06-21 13:27:21

标签: javascript html5-video video.js

(function () {
// Creating and Appending scripts dynamically

function createScript(src) {
    var script = document.createElement('script');
    script.src = src;
    document.getElementsByTagName('head')[0].appendChild(script);
}

// End script tags Here

function createLink(href) {
    var link = document.createElement('link');
    link.href = href;
    link.rel = "stylesheet";
    link.type = "type/css";
    document.getElementsByTagName('head')[0].appendChild(link);
}
createLink('http://vjs.zencdn.net/4.12/video-js.css');
createScript('http://vjs.zencdn.net/4.12/video.js');
createLink('http://localhost/projects/test/bin/videojs.vast.vpaid.min.css');
createScript('http://localhost/projects/test/bin/videojs_4.vast.vpaid.min.js');
createScript('http://localhost/projects/test/bin/es5-shim.js');
createScript('http://localhost/projects/test/bin/ie8fix.js');




// DIV 2 
// Div to hold the video

var divContainer = document.createElement('div');
divContainer.class = 'example-video-container';
divContainer.style.display = 'inline-block';
document.getElementById("video3438445[CB]").appendChild(divContainer);

// End parent Div here (parent of video div/tag)

// Video Player Below 
// Create the video tag for html video player

var video = document.createElement('video');
video.id = 'video';
/*video.width = 300;
video.height = 250;*/
video.className = 'video-js vjs-default-skin';

video.autoplay = true;
video.controls = true;
video.muted = true;
video.preload = 'auto';
video.poster = "http://vjs.zencdn.net/v/oceans.png";
//video.data-setup = '{}'; 

// Function to create sources for video
function addSourceToVideo(element, src, type) {
    var source = document.createElement('source');

    source.src = src;
    source.type = type;

    element.appendChild(source);
}

addSourceToVideo(video, 'http://grochtdreis.de/fuer-jsfiddle/video/sintel_trailer-480.mp4', 'video/mp4');
addSourceToVideo(video, 'http://video-js.zencoder.com/oceans-clip.mp4', 'video/webm');
addSourceToVideo(video, 'http://video-js.zencoder.com/oceans-clip.mp4', 'video/ogg');

var paragraph = document.createElement('p');
paragraph.innerHTML = "Video PlayBack Not Supported";
video.appendChild(paragraph);

video.load();
//video.play();
divContainer.appendChild(video);

// Video player creation ends here
var vt = 'Vast_tag_url';
var vpaidPath = 'http://localhost/projects/test/bin/VPAIDFlash.swf';
setTimeout(myFunction, 1000);
function myFunction() {
    var player = videojs(
            "video", {
                plugins: {
                    "vastClient": {
                        "adTagUrl": vt,
                        "adCancelTimeout": 15000,
                        "adsEnabled": true,
                        "playAdAlways": true,
                        "vpaidFlashLoaderPath": vpaidPath
                    }
                }
            })
}
})();

当我们的头部带有CSS和JS,然后是div且体内具有视频和JS功能时,它可以在常规HTML页面上使用。但是,当我创建一个JS文件并由javascript包含它时,它就无法正常工作。 请建议我我在做什么错。

2 个答案:

答案 0 :(得分:0)

加载脚本的方式是异步的。 您可以将它们直接加载到头部,也可以实现一种方法来侦听加载事件。下面的示例用ES6编写。

function loadScript(src) {
    return new Promise((onFulfilled, onRejected) => {
        const script = document.createElement('script');
        let loaded;

        // set source path to load
        script.setAttribute('src', src);

        script.onreadystatechange = script.onload = () => {
            if (!loaded) {
                onFulfilled(script);
            }
            loaded = true;
        };

        script.onerror = function() {
            // something went wrong
        };

        // append the given script
        document.getElementsByTagName('head')[0].appendChild(script);
    });
}

loadScript('http://url').then(() => {
    // do something now that the script is loaded.
});

现在,您将确保当实现诺言时,脚本已成功加载。

答案 1 :(得分:0)

非常感谢您的回复。我的问题已解决。我为此使用了以下代码。

function loadScriptSync(src) {
    var s = document.createElement('script');
    s.src = src;
    s.type = "text/javascript";
    s.async = false;                                 // <-- this is important
    document.getElementsByTagName('head')[0].appendChild(s);
}

它将同步加载js文件。