Javascript code to autoplay youtube video not working
-
Hi, added js code for youtube video in code module in the popup section. Popup works but video doesn’t show at all.
Below is the code used and it only works flawlessly if not placed on the popup section.
Page source shows that the code is not rendered when the popup opens. Using this code so that the video autoplays in the popup on mobiles/tablets.Much appreciate your help urgently please.
<!– 1. The <iframe> (video player) will replace this <div> tag. –>
<div class=”iframe-container”>
<div id=”player”></div>
</div>
<script>
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement(‘script’);tag.src = “https://www.youtube.com/iframe_api”;
var firstScriptTag = document.getElementsByTagName(‘script’)[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player(‘player’, {
width: ‘100%’,
videoId: ‘SV5EGNPPbDE’,
playerVars: { ‘autoplay’: 1, ‘playsinline’: 1 },
events: {
‘onReady’: onPlayerReady
}
});
}// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.mute();
event.target.playVideo();
}
</script><style>
/* Make the youtube video responsive */
.iframe-container{
position: relative;
width: 100%;
padding-bottom: 56.25%;
height: 0;
}
.iframe-container iframe{
position: absolute;
top:0;
left: 0;
width: 100%;
height: 100%;
}
</style>
- The topic ‘Javascript code to autoplay youtube video not working’ is closed to new replies.