Skip to content

Instantly share code, notes, and snippets.

@xto3na
Last active June 10, 2016 14:44
Show Gist options
  • Save xto3na/5eda9f88a487245cc630d988a496a7d6 to your computer and use it in GitHub Desktop.
Save xto3na/5eda9f88a487245cc630d988a496a7d6 to your computer and use it in GitHub Desktop.
STOP Youtube-video
<div class="video-wr">
<iframe width="490" height="300" src="https://www.youtube.com/embed/2CPY5C-PYPs?enablejsapi=1" frameborder="0" allowfullscreen></iframe>
</div>
<button type="button" class="btn btn-primary stop-btn">PAUSE</button>
<script>
document.addEventListener("DOMContentLoaded", function() {
//Initializing video for Enable jsapi
// For each of them:
$("iframe").each(function() {
// If "enablejsapi" is not set on the iframe's src, set it:
if (this.src.indexOf("enablejsapi") === -1) {
// ...check whether there is already a query string or not:
// (ie. whether to prefix "enablejsapi" with a "?" or an "&")
var prefix = (this.src.indexOf("?") === -1) ? "?" : "&amp;";
this.src += prefix + "enablejsapi=true";
}
});
var func = 'pauseVideo'; // OR 'playVideo' or 'stopVideo' if need to play
$(".stop-btn").on("click", function() {
$("iframe").each(function() {
this.contentWindow.postMessage('{"event":"command","func":"' + func + '","args":""}', '*');
});
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment