1

I have div that shows infront of a youtube video. How can I stop the video when it is clicked? check out my example. http://jsfiddle.net/uprosoft/6wXvA/81/

thanks.

Ronny K
  • 3,641
  • 4
  • 33
  • 43
  • 3
    possible duplicate: http://stackoverflow.com/questions/1094397/how-can-i-stop-a-video-with-javascript-in-youtube – Ram Jul 02 '12 at 08:56

3 Answers3

2

Look here

http://apiblog.youtube.com/2011/01/introducing-javascript-player-api-for.html

You have this function:

 function stopVideo() {
    player.stopVideo();
}
elclanrs
  • 92,861
  • 21
  • 134
  • 171
elo
  • 615
  • 4
  • 11
1

You can't control using iFrame, you need

<object id="ytplayer" style="height: 390px; width: 640px">
    <param name="movie" value="http://www.youtube.com/v/NWHfY_lvKIQ?version=3&enablejsapi=1">
    <param name="allowScriptAccess" value="always">
    <embed id="ytplayer" src="http://www.youtube.com/v/NWHfY_lvKIQ?version=3&enablejsapi=1" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="640" height="390">
</object>

<div onclick="document.getElementById('ytplayer').stopVideo()">Stop</div>
Ashwin Singh
  • 7,197
  • 4
  • 36
  • 55
0

You can't access the DOM from an iframe if the main document and the iframe's one aren't from the same domain.

i.e.: from chrome console:

Unsafe JavaScript attempt to access frame with URL http://www.youtube.com/embed/NWHfY_lvKIQ?wmode=opaque from frame with URL http://fiddle.jshell.net/_display/. Domains, protocols and ports must match.

If you want to control youtube video, you need to embed it into your page.

On a youtube video, click on "Embed", then check this box :

Use old embed code [?]

you'll have the good code :)

Shikiryu
  • 10,180
  • 8
  • 49
  • 75