On Code 1 that uses 1 ClickHandler, YouTube starts playing right away before it is clicked.
On Code 2 that uses 2 ClickHandlers, YouTube doesn’t start playing before it is clicked.
How do I fix the code where YouTube starts playing right away, before it is clicked?
Should Code 1 that uses only 1 ClickHandler, should it be using 2 instead?
Would that fix the issue here?
(Code 1) uses 1 ClickHandler
YouTube Autoplays before it is clicked
Click “Run”
or, YouTube starts after opening the link.
code: https://jsitor.com/88Ub5j2ZjO
(function iife() {
"use strict";
function show(el) {
el.classList.remove("hide");
document.querySelector(".container1").classList.add('slide');
}
function hide(el) {
el.classList.add("hide");
}
function coverClickHandler(evt) {
const cover = evt.currentTarget;
const thewrap = cover.parentNode.querySelector(".container");
hide(cover);
show(thewrap);
}
const cover = document.querySelector(".jacket");
cover.addEventListener("click", coverClickHandler);
}());
const videoPlayer = (function makeVideoPlayer() {
"use strict";
function onPlayerReady(event) {
const player = event.target;
player.setVolume(100); // percent
}
let hasShuffled = false;
function onPlayerStateChange(event) {
const player = event.target;
const shufflePlaylist = true;
if (!hasShuffled) {
player.setShuffle(shufflePlaylist);
player.playVideoAt(0);
hasShuffled = true;
}
}
function addVideo(video) {
const playlist = "0dgNc5S8cLI,mnfmQe8Mv1g,-Xgi_way56U,CHahce95B1g";
new YT.Player(video, {
width: 640,
height: 360,
host: "https://www.youtube-nocookie.com",
playerVars: {
autoplay: 1,
controls: 1,
loop:1,
rel: 0,
iv_load_policy: 3,
cc_load_policy: 0,
fs: 0,
disablekb: 1,
playlist
},
events: {
"onReady": onPlayerReady,
"onStateChange": onPlayerStateChange
}
});
}
function init(opts) {
load.js("https://www.youtube.com/player_api").then(function() {
YT.ready(function() {
addVideo(opts.video);
});
});
}
return {
init
};
}());
videoPlayer.init({
video: document.querySelector(".video")
});
(Code 2) uses 2 ClickHandlers.
YouTube Does Not Autoplay before it is clicked
Which is how it should work.
code: https://jsitor.com/8OeEw1Xoj
(function manageCover() {
"use strict";
function hide(el) {
el.classList.add("hide");
}
function coverClickHandler(evt) {
const cover = evt.currentTarget;
hide(cover);
}
const cover = document.querySelector(".jacket");
cover.addEventListener("click", coverClickHandler);
}());
const videoPlayer = (function makeVideoPlayer() {
"use strict";
function onPlayerReady(event) {
const player = event.target;
player.setVolume(100); // percent
}
let hasShuffled = false;
function onPlayerStateChange(event) {
const player = event.target;
const shufflePlaylist = true;
if (!hasShuffled) {
player.setShuffle(shufflePlaylist);
player.playVideoAt(0);
hasShuffled = true;
}
}
function addVideo(video) {
const playlist = "0dgNc5S8cLI,mnfmQe8Mv1g,-Xgi_way56U,CHahce95B1g";
new YT.Player(video, {
width: 640,
height: 360,
host: "https://www.youtube-nocookie.com",
playerVars: {
autoplay: 1,
controls: 1,
loop: 1,
rel: 0,
iv_load_policy: 3,
cc_load_policy: 0,
fs: 0,
disablekb: 1,
playlist
},
events: {
"onReady": onPlayerReady,
"onStateChange": onPlayerStateChange
}
});
}
function init(opts) {
load.js("https://www.youtube.com/player_api").then(function() {
YT.ready(function() {
addVideo(opts.video);
});
});
}
return {
init
};
}());
(function iife() {
"use strict";
function show(el) {
el.classList.remove("hide");
}
function initPlayer(wrapper) {
videoPlayer.init({
video: wrapper.querySelector(".video")
});
}
function coverClickHandler(evt) {
const wrapper = evt.currentTarget.nextElementSibling;
show(wrapper);
initPlayer(wrapper);
}
const cover = document.querySelector(".jacket");
cover.addEventListener("click", coverClickHandler);
}());