Вопрос

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.

Это было полезно?

Решение

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() {}
}

Другие советы

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top