2016-06-11 4 views
0

これはHTMLファイル内の有効なプレーヤーです。 YT.PlayerState.ENDED a.k.a. event.data == 0のハンドラが、videoIDを格納するクライアントサイドのJavascriptオブジェクト(booth)を参照できるようにしたいが、boothは未定義です。このようなことをするための適切なテクニックは何ですか?ここでYouTube iframe API - 内部からJavascriptオブジェクトを参照していますonPlayerStateChangeイベントハンドラ

function onPlayerStateChange(event) { 
    if (event.data == -1) { 
    player.playVideo(); 
    } 
    if (event.data == 0) { 
    alert("This alert pops up"); 
    alert(window.booth); 
    // Undefined -- even though "booth" is already instantiated in linked JS file 

    /* 
    window.booth.cue.index++; 
    event.target.loadVideoById(window.booth.cue.list[booth.cue.index].id); 
    */ 
    } 
} 

は全体Javascript fileある - ライン16 socket.on('boothCreated'...boothがインスタンス化され、それが作成したプレイヤーにのみ後にされたハンドラである - それは、プレイヤーがこれを参照することができるはずと私に思われます。

+0

リンクされたJSファイルを提供できますか?ブラウザは決して間違っていません。 – Azamantes

+0

@Azamantes、JSファイルを追加したことに気付かなかった場合に備えて、タグ付けしています。 – aweeeezy

答えて

1

私はさらに任意の見ていない必要がある、リンクされたJSファイルの先頭には、すべてを語る:

window.onload = function() { // beginning of function closure 
    var user = null; // these are private variables inside the function 
    var booth = null; // these are private variables inside the function 
    // etc... 

あなたはこれらの変数がwindow.boothなどから見えるようにしたい場合は、あなたがこれを実行する必要があります。

window.onload = function() { // beginning of function closure 
    window.user = null; // these are now public 
    window.booth = null; // these are now public 
    // etc... 

あなたはそれらを非公開にして、外界から見えないようにしました。

また、非公開のまま使用する場合は、ファイルを結合してonPlayerStateChange機能をwindow.onload = function() { ...の内部に配置する必要があります。

+0

ありがとうございます - あなたは素晴らしいです!私はそのような初歩的なJSのことを知らなかったのは恥ずかしいです。 – aweeeezy

+0

心配しないで、我々はすべてそこにいた:) – Azamantes

関連する問題