How can I stop a video with Javascript in Youtube?

Léo Léopold Hertz 준영 picture Léo Léopold Hertz 준영 · Jul 7, 2009 · Viewed 73.9k times · Source

Situation: here, where I pressed some video.

Problem: I try to stop the video by Javascript in the console of Firebug:

player.stopVideo(playerid):Void   [1] [2]

Question: Why does not the command above work?

[1] Source for the part "player.stopVideo():Void"

[2] I looked playerid with Firebug from the source.

Answer

Andrew Martinez picture Andrew Martinez · Jul 13, 2009

Your video is requesting w/ the JSAPI enabled, so you are very close! All you need is a valid reference to the embedded player. Inspecting your page revealed that you are using the HTML DOM element id of "playerid" to identify your player.

Example:

<embed id="playerid" width="100%" height="100%" allowfullscreen="true" allowscriptaccess="always" quality="high" bgcolor="#000000" name="playerid" style="" src="http://www.youtube.com/apiplayerbeta?enablejsapi=1&playerapiid=normalplayer" type="application/x-shockwave-flash">

To obtain a reference to the player and then stop the video use the following code:

var myPlayer = document.getElementById('playerid');
myPlayer.stopVideo();