What I want to do is that when I click on the close button of my modal, it stops me the video that is playing, for the moment the video remains playing in my website, currently it I'm trying this way:
Youtube Object
<center>
<object width="420" height="315" data="https://www.youtube.com/embed/XGSy3_Czz8k">
</object>
</center>
JS code
var tag = document.createElement('script');
tag.src = "//www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubePlayerAPIReady() {
// create the global player from the specific iframe (#video)
player = new YT.Player('videoM', {
events: {
// call this function when player is ready to use
'onReady': onPlayerReady
}
});
}
function onPlayerReady(event) {
onYouTubePlayerAPIReady();
var pauseButton = document.getElementById("cerrarM");
pauseButton.addEventListener("click", function() {
player.stopVideo();
});
}
Button code close
<button class="close" onclick="cerrar(2);" id="cerrarM">×</button>
At the moment I do not perform any action, before I said that the stop event did not exist in the context, but it does not tell me that and the video is still heard in the background.