title: Stop YouTube video within iFrame on external Button click
body: <p>I know there are similar questions to this, but I haven't quite been able to get it to  work.</p>
  
  <p>This code shows a YouTube video which is within an iframe. When a button (emsbinstartbutton) is clicked the video is hidden and replaced with a placeholder.  If this place holder (<code>threebytwo</code>) is closed the video should show again.</p>
  
  <p>At the moment this works fine except that when the video is hidden it will continue to play (you can hear the audio and when you go back you can see it is still playing). The video should be stopped when hidden.</p>
  
  <pre><code> &lt;%@ Page Language="C#" MasterPageFile="~/Views/Shared/Widget.Master" Inherits="System.Web.Mvc.ViewPage&lt;WidgetView&gt;" %&gt;
  
  &lt;%@ Import Namespace="WidgetData" %&gt;
  &lt;asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"&gt;
      &lt;%= Html.WidgetTitle(Model) %&gt;
  &lt;/asp:Content&gt;
  &lt;asp:Content ID="WidgetStyle" ContentPlaceHolderID="StyleContent" runat="server"&gt;
      &lt;%= Html.WidgetStyle(Model) %&gt;
      &lt;%= Html.WidgetScripts(Model) %&gt;
      &lt;script type="text/javascript"&gt;
  
          // when they click on the buy now button we will hide the title and show the widget
          //detach work to stop vid but can t get it back.....
          $(document).ready(function () {
              $("#emsbinstartbutton").click(function () {
                 $("#divbuyonlineVid").hide();
                  $(".divbuyonlineVid").hide();
                  $("#threebytwo").show();
              });
          });
  
  
          // when they click on the x the widget should be hidden and the video shows again
          $(document).ready(function () {
              $("#closebtn").click(function () {
                  $("#divbuyonlineVid").show();
                  $(".divbuyonlineVid").show();
                  $("#threebytwo").hide();               
              });
          });
  
      &lt;/script&gt;
      &lt;!-- playing with additional CSS needed to get the page behaving the way I want Will move this into the widget style once proven --&gt;
      &lt;style type="text/css"&gt;
          #threebytwo
          {
              display: none;
          }
          #emsbin2startbutton
          {
              cursor: pointer;
          }
      &lt;/style&gt;
  &lt;/asp:Content&gt;
  &lt;asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"&gt;
      &lt;div class="divbuyonlineVid"&gt;
          &lt;!-- IMPORTANT the youtube video has to include &amp;wmode=opaque at the end of the link so that the buynow button is above the video --&gt;
          &lt;img class="emsbinstarttext" id="emsbinstarttext" src="http://static.e-talemedia.net/content/images/DoveYoutubeMessage.png" /&gt;
          &lt;img class="emsbinstartbutton" id="emsbinstartbutton" src="http://static.e-talemedia.net/content/images/DoveYoutubeBuyNowRed.png" /&gt;
          alt="" /&gt;
          &lt;div class="divVidContainer"&gt;
              &lt;iframe id="player" width="640" height="360" src="http://www.youtube.com/embed/AvywK19yVEk?feature=player_embedded&amp;wmode=opaque&amp;enablejsapi=1"
                  frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div id="threebytwo" class="threebytwo"&gt;
          &lt;img class="closebtn" id="closebtn" src="http://static.e-talemedia.net/content/images/DoveYoutubeExit.png" /&gt;
          &lt;div class="threebytwomiddle"&gt;
              &lt;ul&gt;
                  &lt;% if (Model.ProductNotFound)
                     { %&gt;
                  &lt;!-- List the retailers as we have none in stock --&gt;
                  &lt;%= Html.WidgetProductNotFoundList(Model) %&gt;
                  &lt;% }
                     else
                     { %&gt;
                  &lt;%= Html.WidgetProductFoundList(Model) %&gt;
                  &lt;% } %&gt;
              &lt;/ul&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;%= Html.WidgetImpressionCode2(Model) %&gt;
      &lt;%= Html.WidgetTrackingCode(Model) %&gt;
  &lt;/asp:Content&gt;
  </code></pre>
  
  <p>I have tried the following:</p>
  
  <h3>using .detach and append</h3>
  
  <pre><code>  // when they click on the buy now button we will hide the title and show the widget
          //detach work to stop vid but can t get it back.....
          $(document).ready(function () {
              $("#emsbinstartbutton").click(function () {
                  $holdera = $("#divbuyonlineVid").detach();
                  $holderb = $(".divbuyonlineVid").detach();
                  $("#threebytwo").show();
              });
          });
  
  
          // when they click on the x the widget should be hidden and the video shows again
          $(document).ready(function () {
              $("#closebtn").click(function () {    
                  $("#threebytwo").hide();
                  alert($holdera);
                  $("#divbuyonlineVid").append($holdera);
                  alert($holderb);
                  $(".divbuyonlineVid").append($holderb);
              });
          });
  </code></pre>
  
  <p>Even though the video stops on click, when I try to go back (Append) I get an object error pop up.</p>
  
  <h3>Stop and StopVideo</h3>
  
  <p>So in the iframe, adding <code>&amp;enablejsapi=1</code>:</p>
  
  <pre><code> &lt;iframe id="player" width="640" height="360" src="http://www.youtube.com/embed/AvywK19yVEk?feature=player_embedded&amp;wmode=opaque&amp;enablejsapi=1"
                  frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;
  </code></pre>
  
  <p>and in the jQuery:</p>
  
  <pre><code> $(document).ready(function () {
              $("#emsbinstartbutton").click(function () {
                  $holdera = $("#divbuyonlineVid").hide();
                  $holderb = $(".divbuyonlineVid").hide();
                  $("#player").stop();
                  $("#threebytwo").show();
              });
          });
  </code></pre>
  
  <p>Which doesn't change anything. And also:</p>
  
  <pre><code> $(document).ready(function () {
              $("#emsbinstartbutton").click(function () {
                  $("#divbuyonlineVid").hide();
                  $(".divbuyonlineVid").hide();
                  $("#player").stopvideo();
                  $("#threebytwo").show();
              });
          });
  </code></pre>
  
  <p>On the button click you can still hear the audio, but threebytwo does not show.</p>
  
  <p>I have also added this <code>&lt;script&gt;</code>:</p>
  
  <pre><code>&lt;script type="text/javascript" src="http://www.youtube.com/player_api" &gt;&lt;/script&gt;
  </code></pre>
  
  <p>Can anyone point me in the right direction or see where I'm going wrong?</p>
  
  <p><strong>Edit:  shabeer90 describes how to stop the  youtube video which got me on my way but if you just wanted to pause it and keep it in the background the following code may be useful:</strong></p>
  
  <p><strong>jQuery:</strong></p>
  
  <pre><code> &lt;script type="text/javascript"&gt;
          $(document).ready(function () {
              var obj = $('object')
       .wrap('&lt;div id="test"&gt;&lt;/div&gt;')
       .find('embed').attr('src', function (i, s) { return s + '&amp;enablejsapi=1&amp;version=3' }).end()
       .find('param[name=movie]').attr('value', function (i, v) { return v + '&amp;enablejsapi=1&amp;version=3' }).end()
       .detach()
       .appendTo($('#test'));
  
              $(document).ready(function () {
                  $("#emsbinstartbutton").click(function () {
                      //Then assign the src to null, this then stops the video been playing
                      obj.find('embed')[0].pauseVideo();
                      $("body").append($("&lt;div&gt;").css({
                          position: "fixed"
          , width: "640px"
          , height: "425px"
          , "background-color": "#000"
          , opacity: 0.6
          , "z-index": 999
          , top: 0
          , left: 0
                      }).attr("id", "page-cover"));
  
                      $("#threebytwo").show();
                  });
              });
  
              // when they click on the x the widget should be hidden and the video shows again
              $(document).ready(function () {
                  $("#closebtn").click(function () {
                      //                     $("#divbuyonlineVid").show();
                      //                     $(".divbuyonlineVid").show();
                      $("#threebytwo").hide();
                      $("#page-cover").detach();
                  });
              });
  
          });
      &lt;/script&gt;
  </code></pre>
  
  <p><strong>html:</strong></p>
  
  <pre><code>&lt;div class="divbuyonlineVid"&gt;
          &lt;img class="emsbinstarttext" id="emsbinstarttext" src="http://static.e-talemedia.net/content/images/DoveYoutubeMessage.png" /&gt;
          &lt;img class="emsbinstartbutton" id="emsbinstartbutton" src="http://static.e-talemedia.net/content/images/DoveYoutubeBuyNowRed.png"
              alt="" /&gt;
          &lt;div class="divVidContainer"&gt;
              &lt;object width="640" height="385"&gt;
                  &lt;%--IMPORTANT - the embed video format has to change so that it is exactly like below with the /v/ and &amp;amp;hl=en_US&amp;amp;fs=1  and  &amp;wmode=opaque for the mask--%&gt;
                  &lt;param name="movie" value="http://www.youtube.com/v/fBZTbCrM2eQ&amp;amp;hl=en_US&amp;amp;fs=1&amp;wmode=opaque "&gt;
                  &lt;/param&gt;
                  &lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;
                  &lt;param name="allowscriptaccess" value="always"&gt;&lt;/param&gt;
                  &lt;embed src="http://www.youtube.com/v/fBZTbCrM2eQ&amp;amp;hl=en_US&amp;amp;fs=1&amp;wmode=opaque"
                      type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true"
                      width="640" height="385" wmode="Opaque"&gt;&lt;/embed&gt;
              &lt;/object&gt;
  
          &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>Many thanks.</p>