未捕获的ReferenceError:未定义videojs。 videojs异步加载

时间:2016-08-08 11:27:52

标签: javascript

  1. 所以我有一个js文件如下: -
  2. (function(){
        console.log("Starting Point");
    
        // Creating and Appending scripts dynamically
    
        function createScript(src) {
          console.log("script for js");	
          var script = document.createElement('script');
          script.src = src;
          document.getElementsByTagName('head')[0].appendChild(script);
        }
        
        // End script tags Here
    
        function createLink(href) {
          console.log("script for links");	
          var link = document.createElement('link');  
          link.href = href;
          link.rel = "stylesheet";
          link.type = "type/css";
          document.getElementsByTagName('head')[0].appendChild(link);
        }     
    
        // DIV 1
        // Create a main Div to hold everything
    
        createScript('http://vjs.zencdn.net/4.7.1/video.js');
        createScript('lib/videojs-contrib-ads/videojs.ads.js');
        createScript('lib/vast-client.js');
        createScript('videojs.vast.js');
    
        createLink('http://vjs.zencdn.net/4.7.1/video-js.css');
        createLink('lib/videojs-contrib-ads/videojs.ads.css');
        createLink('videojs.vast.css');
    
      
        console.log("Create mainDiv");
        var mainDiv = document.createElement('div');
        mainDiv.id  = 'ad_placement';
        // mainDiv.style.float = "right";
        var currentScript = document.currentScript;
        currentScript.parentElement.insertBefore(mainDiv, currentScript);
        // document.body.appendChild(mainDiv);
        console.log("End MainDiv"); 
    
        // End main Div here
    
    
        // DIV 2 
        // Div to hold the video
        
        console.log("Create innerDiv")
        var divContainer = document.createElement('div');
        divContainer.class = 'example-video-container';
        divContainer.style.display = 'inline-block';
        mainDiv.appendChild(divContainer);   
        console.log("Create innerDiv")
    
        // End parent Div here (parent of video div/tag)
    
        // Video Player Below 
        // Create the video tag for html video player
        console.log("Video Creation Started");
    
        var video = document.createElement('video');
        video.id = 'vid2';
        video.width = 300;
        video.height = 250;
        video.class = 'video-js vjs-default-skin';
        
        video.autoplay = true;
        video.controls = true;
        video.muted = true;
        video.preload = 'auto';
        video.poster = "http://www.jqueryscript.net/images/Simplest-Responsive-jQuery-Image-Lightbox-Plugin-simple-lightbox.jpg";
        //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');
    
        console.log("Video Creation End");
      
        console.log("Para Started");
    
        var paragraph = document.createElement('p');
        paragraph.innerHTML = "Video PlayBack Not Supported";
        video.appendChild(paragraph);
    
        video.load();
        //video.play();
        divContainer.appendChild(video);
    
        console.log("Para end");
        // Video player creation ends here
        
        console.log("Before calling videojs");
        var vid2 = videojs('vid2');
        console.log("After calling videojs");
        vid2.muted(true);
        
        vid2.ads({
          timeout: 5000
        });
        
        vid2.vast({
          url: 'vast_tag_url'
        });
    
        vid2.on('readyforpreroll', function() {
          vid2.ads.startLinearAdMode(); 
          vid2.one('ended', function() {
            vid2.ads.endLinearAdMode();
            console.log("ad finished");
            document.getElementById('adPlacement').innerHTML="<img src='https://wallazee.global.ssl.fastly.net/images/variant/20140109-11d5e6789afea899c324f5d4cbfa00eca24c58c8b790e1d6305c36e-1024.png' width=300 height=250 border=0/>";
          });
    
        });
    
        vid2.on('adtimeout', function() {
            console.log("timout happened")
            document.getElementById('adPlacement').innerHTML="<img src='https://wallazee.global.ssl.fastly.net/images/variant/20140109-11d5e6789afea899c324f5d4cbfa00eca24c58c8b790e1d6305c36e-1024.png' width=300 height=250 border=0/>";
        });
    
        console.log("End of file");
    
    })();

    1. 所以当我在html页面中包含此文件时,我收到video.js未定义错误。需要一个JavaScript解决方案。 (我不能使用jquery)
    2. 我错了。当我们在头部有css和js然后在体内有视频和js函数的div时,它在正常的html页面上工作。
      1. 当我创建了一个js并导入然后它出了问题

0 个答案:

没有答案