2016-11-14 59 views
0

我使用'video.js'作爲外部JavaScript文件。正如你所看到的,videoID必須在這個文件中給出。但我想從我的HTML文件傳遞這個值,所以我不需要在HTML中包含所有的JavaScript。如何將videoID傳遞到外部文件

的JavaScript(另存爲的Video.js)

function onYouTubeIframeAPIReady() { 
    var player; 
    player = new YT.Player('playVid', { 
    videoId: 'HERE COMES THE VIDEO ID', // YouTube Video ID 
    width: 640,    // Player width (in px) 
    height: 360,    // Player height (in px) 
    playerVars: { 
     autoplay: 1,  // Auto-play the video on load 
     controls: 1,  // Show pause/play buttons in player 
     showinfo: 0,  // Hide the video title 
     modestbranding: 1, // Hide the Youtube Logo 
     loop: 1,   // Run the video in a loop 
     fs: 0,    // Hide the full screen button 
     cc_load_policy: 0, // Hide closed captions 
     iv_load_policy: 1, // Hide the Video Annotations 
     autohide: 0   // Hide video controls when playing 
    }, 
    events: { 
     onReady: function(e) { 
     e.target.mute(); 
     } 
    } 
    }); 
} 

HTML

<html> 
    <head> 
    <script type="text/javascript" async src="https://www.youtube.com/iframe_api"></script> 
    <script type="text/javascript" src="video.js"></script> 
    </head> 
    <body> 
    <div id="playVid"></div> 
    </body> 
</html> 

這完美的作品,但同樣我的問題:

我怎樣才能納入videoID在我的HTML中,而(JavaScript)代碼的其餘部分停留在extern一個JavaScript文件(video.js)。

+0

這真的取決於你想如何把'videoID'在_html_。如果你只是想將'videoID'包含爲一個JavaScript變量,那麼到目前爲止,答案'Tony Sejas','Hayden'都將完成這項工作。但假設你有一個鏈接,並且你想在點擊該鏈接時顯示視頻,那麼你必須採取與這兩個答案稍微相同的方法,但是編輯它們以符合你的願望 – EhsanT

回答

0
<html> 
<head> 
</head> 
<body> 
<div id="playVid"></div> 
<script> 
    var youtubVideoId = 'YOUR_VIDEO_ID'; 
</script> 
<script type="text/javascript" async src="https://www.youtube.com/iframe_api"></script> 
<script type="text/javascript" src="video.js"></script> 
</body> 
</html> 

很確定會這樣做。然後只需引用video.js文件中的JS變量。

function onYouTubeIframeAPIReady() { 
    var player; 
    player = new YT.Player('playVid', { 
    videoId: youtubVideoId, // YouTube Video ID 
    }); 
} 
0

你只需要在你的html中創建一個javascript var。而在你的.js文件中,你只需使用你創建的那個var。 像這樣:

function onYouTubeIframeAPIReady() { 
 
    var player; 
 
    player = new YT.Player('playVid', { 
 
    videoId: myVideoIdVar, // YouTube Video ID 
 
    width: 640,    // Player width (in px) 
 
    height: 360,    // Player height (in px) 
 
    playerVars: { 
 
     autoplay: 1,  // Auto-play the video on load 
 
     controls: 1,  // Show pause/play buttons in player 
 
     showinfo: 0,  // Hide the video title 
 
     modestbranding: 1, // Hide the Youtube Logo 
 
     loop: 1,   // Run the video in a loop 
 
     fs: 0,    // Hide the full screen button 
 
     cc_load_policy: 0, // Hide closed captions 
 
     iv_load_policy: 1, // Hide the Video Annotations 
 
     autohide: 0   // Hide video controls when playing 
 
    }, 
 
    events: { 
 
     onReady: function(e) { 
 
     e.target.mute(); 
 
     } 
 
    } 
 
    }); 
 
}
<html> 
 
<head> 
 
<script type="text/javascript" async src="https://www.youtube.com/iframe_api"></script> 
 
<script type="text/javascript"> 
 
    var myVideoIdVar = 'HERE COMES THE VIDEO ID' 
 
</script> 
 
<script type="text/javascript" src="video.js"></script> 
 
</head> 
 
<body> 
 
<div id="playVid"></div> 
 
</body> 
 
</html>

相關問題