YouTube video

by MichaelTeam

HTML

<div id="embed" class="embed-container">Test</div>

<br><br><br>

<form name="link" onsubmit="showYouTube(true); return false;">
<input name="yt" type="text" value="" size="50" placeholder="Paste a YouTube link here..." />
<button>Watch video!</button>
</form>
<br><br><br>

JavaScript

function showYouTube (input=false) {
	let yt = 'BmlB8y5Sig8';
  let ytlink = '';
	if (input) {
  	ytlink = document.forms.link.yt.value;
    window.scrollTo(0, 0);
  }
  if (!ytlink) {
  	let urlParams = new URLSearchParams(window.location.search);
		ytlink = urlParams.get('yt');
  }
  if (ytlink) {
		let video = ytlink.match(/v=(.+)/);
		if (video) yt = video[1];
	}
	document.querySelector("#embed").innerHTML = '<iframe src="https://www.youtube-nocookie.com/embed/'+yt+'" width="853" height="480" frameborder="0" webkitallowfullscreen="1" mozallowfullscreen="1" allowfullscreen="1"></iframe>';
}