Quick answer
Closing a modal only hides it, so an embedded YouTube iframe keeps playing. To stop it, either pause it by sending a postMessage command to the iframe (add enablejsapi=1 to the embed URL), or reset the iframe’s src when the modal closes. For Bootstrap 5, run that code in the hidden.bs.modal event; for a native <dialog>, use its close event.
Key takeaways
- The video keeps playing because the modal is hidden, not removed.
- Add ?enablejsapi=1 to the embed URL so you can pause it with postMessage.
- Resetting the src always stops the video and works without the API.
- Best for page speed: load the iframe only when the modal opens.
- Use youtube-nocookie.com for a privacy-friendlier embed.
You put a YouTube video inside a popup. The user closes the popup, and the sound keeps playing in the background. This happens because closing a modal usually just hides it with CSS. The iframe is still on the page, so the video keeps going. Here are three ways to fix it, from simplest to most flexible.
Method 1: Pause the video with postMessage (keeps the position)
Add enablejsapi=1 to the embed URL:
<iframe id="promoVideo"
src="https://www.youtube-nocookie.com/embed/VIDEO_ID?enablejsapi=1"
title="Product demo" allow="autoplay; encrypted-media; picture-in-picture"
allowfullscreen></iframe>Then send a pause command when the modal closes:
function pauseYouTube(iframe) {
iframe.contentWindow.postMessage(
JSON.stringify({ event: 'command', func: 'pauseVideo', args: [] }),
'*'
);
}Use 'stopVideo' instead of 'pauseVideo' to stop and reset. If the user reopens the modal, pausing lets them continue where they left off.
Method 2: Reset the iframe src (always works)
function stopYouTube(iframe) {
const src = iframe.src;
iframe.src = '';
iframe.src = src;
}This reloads the player, so the video starts from the beginning next time. It needs no API parameter, which makes it the most reliable fallback.
Method 3: Load the video only when the modal opens (fastest page)
Embedding YouTube adds several hundred kilobytes of scripts to your page even if nobody plays the video, which can hurt Core Web Vitals. Instead, keep the URL in a data attribute and inject the iframe on open, then remove it on close:
<div class="video-wrap" data-yt="VIDEO_ID"></div>
function openVideo(wrap) {
wrap.innerHTML = '<iframe src="https://www.youtube-nocookie.com/embed/' +
wrap.dataset.yt + '?autoplay=1" title="Video" ' +
'allow="autoplay; encrypted-media; picture-in-picture" allowfullscreen></iframe>';
}
function closeVideo(wrap) { wrap.innerHTML = ''; }Need a website that turns visitors into enquiries?
My team builds fast, secure, SEO-ready websites and web apps for growing businesses.
How do I stop YouTube in a Bootstrap 5 modal?
Bootstrap 5 fires hidden.bs.modal after the modal closes. No jQuery needed:
document.querySelectorAll('.modal').forEach(function (modal) {
modal.addEventListener('hidden.bs.modal', function () {
modal.querySelectorAll('iframe[src*="youtube"]').forEach(stopYouTube);
});
});How do I do it with jQuery (Bootstrap 3 or 4)?
$('.modal').on('hidden.bs.modal', function () {
$(this).find('iframe[src*="youtube"]').each(function () {
var src = $(this).attr('src');
$(this).attr('src', '').attr('src', src);
});
});How do I do it with the native <dialog> element?
Modern browsers support <dialog> with built-in accessibility and the Escape key. Listen for its close event:
const dlg = document.getElementById('videoDialog');
dlg.addEventListener('close', function () {
dlg.querySelectorAll('iframe').forEach(stopYouTube);
});
// open with dlg.showModal(); close with dlg.close()Why doesn’t my pause command work?
- Missing
enablejsapi=1in the embed URL. Without it, the player ignores postMessage commands. - The iframe hasn’t finished loading. Commands sent too early are ignored; Method 2 is safer here.
- Wrong selector. Check the iframe really contains “youtube” in its src (watch out for youtube-nocookie.com, which still matches).
- Autoplay blocked. Browsers block autoplay with sound; add
mute=1if you autoplay.
The same approach works for Vimeo using its Player API or by resetting the src.
Frequently asked questions
Why does a YouTube video keep playing after closing a modal?
Most modals hide content with CSS rather than removing it, so the iframe and its audio keep running. You need JavaScript to pause the player or reload the iframe when the modal closes.
What is enablejsapi=1 in a YouTube URL?
It enables the YouTube IFrame Player API, which lets your page send commands such as pauseVideo, stopVideo and playVideo to the embedded player.
How do I stop a YouTube video in a Bootstrap 5 modal?
Listen for the hidden.bs.modal event on the modal and either send a pauseVideo command via postMessage or reset the iframe src.
Does embedding YouTube slow down my website?
Yes. Each embed loads extra scripts. Load the iframe only when needed or use a lightweight facade (a thumbnail that loads the player on click) to protect page speed.