Question

I have a function such as this one:

function CreateYoutubePlayer(alpha, bravo, charlie, delta, echo) {
    var s = document.createElement("script");
    s.src = "//www.youtube.com/iframe_api";
}

The loaded script requires a global function called onYouTubeIframeAPIReady. How do I create this function within the above function so that it is globally accessible.

Was it helpful?

Solution

you can attach the function to the global window object:

function CreateYoutubePlayer(alpha, bravo, charlie, delta, echo) {
    var s = document.createElement("script");
    s.src = "//www.youtube.com/iframe_api";

    window.onYouTubeIframeAPIReady = function() {}
}

OTHER TIPS

You can declare onYouTubeIframeAPIReady outside of the CreateYoutubePlayer function as an empty variable. This will create a global variable, and you can then populate that function within CreateYoutubePlayer

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top