I have three YouTube videos that I want to stop when a user clicks a link to a page. this is my code
var tag = document.createElement('script'); tag.src = "//www.youtube.com/iframe_api"; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); var youtubePlayer1; var youtubePlayer2; var youtubePlayer3; function onYouTubeIframeAPIReady() { youtubePlayer1 = new YT.Player('firstPlayer', { }); youtubePlayer2 = new YT.Player('secondPlayer', { }); youtubePlayer3 = new YT.Player('thirdPlayer', { }); } function stopVideo() { if (youtubePlayer1 != null) { youtubePlayer1.stopVideo(); } if (youtubePlayer2 != null) { youtubePlayer2.stopVideo(); } if (youtubePlayer3 != null) { youtubePlayer3.stopVideo(); } }
This is the html code
<div id="blog"> <ul> <li> <h3> <strong>ืืืงืช ืงืืืืช - ืืืจืงืื ืืกืืืืช...</strong></h3> <br /> <iframe id="firstPlayer" width="800" height="485" src="http://www.youtube.com/embed/F0eR1KFkt58" style="border:0" ></iframe> <br /> <br /> <img src="images/bg3.PNG" alt="" /><p> <span>ืชืืืจ ืืืืืื: </span>ืืงืกื ืืืืืช ืืืืืื, ืชืืจืื</p> </li> <li> <h3> <strong>ืืืงืช ืงืืืืช - ืืืจืงืื ืืฉืจืืืืช ืืืืจืคืช...</strong></h3> <br /> <iframe id="secondPlayer" width="800" height="485" src="http://www.youtube.com/embed/mPTX4guU1W8" style="border:0" ></iframe> <br /> <br /> <img src="images/bg3.PNG" alt="" /><p> <span>ืชืืืจ ืืืืืื: </span>ืืงืกื ืืืืืช ืืืืืื, ืชืืจืื</p> </li> <li> <h3> <strong>ืืืงืช ืงืืืืช - ืืืื ืืฉืืื...</strong></h3> <br /> <iframe id="thirdPlayer" width="800" height="485" src="http://www.youtube.com/embed/E-_ONZOcScU" style="border:0"></iframe> <br /> <br /> <img src="images/bg3.PNG" alt="" /><p> <span>ืชืืืจ ืืืืืื: </span>ืืงืกื ืืืืืช ืืืืืื, ืชืืจืื</p> </li> </ul> </div>
when a user clicks on a link that calls the stopVideo function, which goes through all the players and stops them.
for some reason I can only make it work on the youtubePlayer2 object, what am I doing wrong here?
Remember, when I debug an application using the chrome debugger, I see that the objects are defined and that the function is being called.
source share