1

Suppose a player object has been defined from another javascript module youtube.js like below:

  var player;
  function onYouTubeIframeAPIReady() {
    player = new YT.Player('initial-video', {
      events: {
        'onReady': onPlayerReady,
        'onStateChange': onPlayerStateChange
      }
    });
  }

How do I access this player from some_other_js_file.js so that I can load another video? Is it possible to define a new variable in some_other_js_file.js that points to the same player object? If it is not, what could be a possible workaround?

Maximus S
  • 10,759
  • 19
  • 75
  • 154

1 Answers1

0

assuming player is defined within an iframe, you can access it from the parent page using its contentwindow property. all global variables within the iFrame will be accesible from the contentWindow

from within some_other_js_file.js

  youtubeiFrame = document.getElementById('youtube-frame-id').contentWindow;

  alert(youtubeiFrame.player)

does this match your situation?

EDIT this answer seems to address accessing a youtube player from parent iframe with a jsFiddle example

Community
  • 1
  • 1
actual_kangaroo
  • 5,971
  • 2
  • 31
  • 45