Computer >> Computer tutorials >  >> Programming >> Javascript

How can I stop a video with JavaScript in Youtube?


To stop a video you need a valid reference to the embedded player. With Firebug, inspect your page and HTML DOM element id of “playerid” should identify the player.

Here’s an example:

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

To stop the video with JavaScript, you need the reference to the player (player_id) and try the following code snippet:

var player = document.getElementById('player_id');
player.stopVideo();