Play Iframe Video On Click A Link Javascript August 30, 2023 Post a Comment I have used a iframe video in my web page. This is my html code Solution 1: This works, it appends autoplay=1 to the url causing the video to start playing. addendum: If your video's source does not already have a querystring then it would be prudent to add a ? instead of a &, as is sometimes the case. This can be done by looking for its existence.<iframeid="video1"width="520"height="360"src="http://www.youtube.com/embed/TJ2X4dFhAC0?enablejsapi"frameborder="0"allowtransparency="true"allowfullscreen></iframe><ahref="#"id="playvideo">Play video</a><script>//use .one to ensure this only happens once $("#playvideo").one(function(){ //as noted in addendum, check for querystring exitencevar symbol = $("#video1")[0].src.indexOf("?") > -1 ? "&" : "?"; //modify source to autoplay and start video $("#video1")[0].src += symbol + "autoplay=1"; }); </script>CopyHowever, most people inherently understand that if they want a video to play, they will just click on it and I would suggest just leaving that to them or starting the video off with autoplay.Also need to mention that autoplay does not work on mobile devices (powered by Android or iOS)Solution 2: I correctly set in end src - ?autoplay=1<iframeid="video1"width="450"height="280"src="http://www.youtube.com/embed/TJ2X4dFhAC0?enablejsapi"frameborder="0"allowtransparency="true"allowfullscreen></iframe><ahref="#"id="playvideo">Play button</a><script> $("#playvideo").click(function(){ $("#video1")[0].src += "?autoplay=1"; }); </script>CopySolution 3: Here is another example. Check this here: https://codepen.io/rearmustak/pen/VXXOBrconstPlayer = document.getElementById('player2'); constPlayBtn = document.getElementById('play'); const stopBtn = document.getElementById('stop'); let times = 0, playY; const playVideo = PlayBtn.addEventListener( 'click' , () => { if(times == 0){ playY = Player.src += '?autoplay=1'; times = 1; } }); stopBtn.addEventListener( 'click' , () => { playY = playY.slice(0, -11); Player.src = playY times = 0; });Copy.video-frame{ overflow: hidden; margin-bottom: 10px; } button{ border: none; background-color: #e75252; color: #ffffff; padding: 10px15px; border-radius: 3px; cursor: pointer; } button:focus{ outline: 0; } #stop{ background-color: #ff0002; }Copy<h1>Youtube video Play/Stop</h1><divclass="video-frame"><iframeid="player2"width="560"height="315"src="https://www.youtube.com/embed/cs1e0fRyI18"frameborder="0"allow="autoplay; encrypted-media"allowfullscreen></iframe></div><buttonid="play">Play</button><buttonid="stop">Stop</button>CopySolution 4: since the first answer is already 3 years old, let me point to the Youtube Player API. With that you can remote control your embedded player. See https://developers.google.com/youtube/iframe_api_reference?hl=enWith a small adjustment, you can start the video via link and without reloading the entire iframe:<!DOCTYPE html><html><body><!-- 1. The <iframe> (and video player) will replace this <div> tag. --><divid="player"></div><!-- The Play-Link will appear in that div after the video was loaded --><divid="play"></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; functiononYouTubeIframeAPIReady() { player = newYT.Player('player', { height: '390', width: '640', videoId: 'M7lc1UVf-VE', events: { 'onReady': onPlayerReady, 'onStateChange': onPlayerStateChange } }); } // 4. The API will call this function when the video player is ready.functiononPlayerReady(event) { //event.target.playVideo();document.getElementById('play').innerHTML = '<a href="#" onclick="play();">Play Video</a>'; } functionplay(){ player.playVideo(); } // 5. The API calls this function when the player's state changes.// The function indicates that when playing a video (state=1),// the player should play for six seconds and then stop.var done = false; functiononPlayerStateChange(event) { if (event.data == YT.PlayerState.PLAYING && !done) { setTimeout(stopVideo, 6000); done = true; } } functionstopVideo() { player.stopVideo(); } </script></body></html>CopySolution 5: here is link to example http://jsfiddle.net/WYwv2/5/Check out this <!DOCTYPE html><html><body><ahref="#"id="playvideo"onclick="playme()">Play video</a><iframeid="video1"width="520"height="360"frameborder="0" ></iframe><script>functionplayme() { document.getElementById("video1").src = 'http://www.w3schools.com/tags/mov_bbb.mp4'; } </script></body></html> CopyWe set the source of video dynamically. Share Post a Comment for "Play Iframe Video On Click A Link Javascript"
Post a Comment for "Play Iframe Video On Click A Link Javascript"