Skip to content

Black Friday 50% off your first year on all premium plugins and the Suite.

See the deals

Stop a video playing when a Divi popup closes

Why a YouTube, Vimeo or self-hosted video keeps playing after a Divi popup closes, the JavaScript fix, and how to avoid it entirely.

4 min read · Updated 4 November 2026

You put a video in a popup, a visitor closes it, and the sound carries on from somewhere they can no longer see. It is one of the most common popup complaints across every builder, and the Divi answers you find are usually “add some JavaScript” without the JavaScript.

Short answer

It depends on how the popup closes.

  • Divi 5 Hide Element interaction: pauses video and reloads YouTube/Vimeo embeds itself (tested on Divi 5.2)
  • A popup hidden with CSS or a class, or closed some other way: keeps playing until you stop it with code
  • Either way, an embed in a hidden native popup still loads on every page view
  • Divi Smart Popup: stops media on every close route and loads embeds only when the popup opens

Why it happens

Most popups close by hiding an element — display:none or a class. Hiding is not stopping: a <video> element keeps playing while hidden, and a YouTube or Vimeo embed is a separate page inside an iframe that knows nothing about your popup.

Divi 5’s own Hide Element (and Toggle Visibility) interaction is the exception. In Divi 5.2 it pauses <video> elements in the hidden section and reloads YouTube, Vimeo, Dailymotion and Facebook iframes without autoplay. We confirmed the pause in a test: a video playing inside a native popup was paused when its Close button ran Hide Element. So if every way your native popup closes is a Hide Element interaction, you don’t need the code below.

So something has to run when the popup closes and tell the player to stop — on every way it can close: the close button, a click on the overlay, the Escape key, an automatic close.

The fix by hand

Give the popup section a CSS class (for example my-popup) and its close button another (my-popup-close). Then add this in Divi → Theme Options → Integration (body code), inside a script tag:

document.addEventListener( 'click', function ( e ) {
	if ( ! e.target.closest( '.my-popup-close' ) ) {
		return;
	}
	var box = document.querySelector( '.my-popup' );
	// Self-hosted video and audio: pause.
	box.querySelectorAll( 'video, audio' ).forEach( function ( m ) {
		m.pause();
	} );
	// YouTube and Vimeo: reloading the iframe stops playback.
	box.querySelectorAll( 'iframe' ).forEach( function ( f ) {
		f.src = f.src;
	} );
} );

Reloading the iframe is the simplest reliable stop for embeds and needs no API. If you would rather pause and keep the position, YouTube accepts a pauseVideo command through postMessage when the embed URL has enablejsapi=1, and Vimeo’s player accepts a pause method the same way — but that means controlling the embed URL, which Divi’s Video module does for you.

Remember to run the same code for every other close route you build (overlay click, Escape), or the video will only stop sometimes.

Don’t load the video until it opens

The other half of the problem is weight: an embed in a hidden popup loads on every page view, even for visitors who never open it. Adding loading="lazy" to the iframe helps in most browsers, because hidden content is not loaded until it is needed.

With Divi Smart Popup

Divi Smart Popup does both for you. Whenever a popup closes — close button, overlay, Escape, #d5pe-close, automatic close or another popup taking its place — it pauses every <video> and <audio> element inside and reloads YouTube and Vimeo iframes, which stops them. Images and iframes in a popup are marked to load lazily, so a video only loads when its popup opens. The Video templates (brand video, video lightbox, video announcement) are a ready-made starting point.

If you use another player, listen for the popup’s close event:

document.addEventListener( 'd5pe:close', function ( e ) {
	if ( 123 === e.detail.id ) {
		// Stop your custom player here.
	}
} );

Where Divi Smart Popup fits

Video popups that stop, and don’t slow the page

Divi Smart Popup handles both halves: media stops however the popup closes — close button, overlay, Escape, #d5pe-close, automatic close — and images and embeds load only when it opens.

  • Pauses video and audio; reloads YouTube and Vimeo iframes
  • Lazy-loads embeds until the popup opens
  • Video templates to start from

See Divi Smart PopupTry the live demos

Divi Smart Popup Layout and style step with seven popup types: modal, slide-in, top bar, bottom bar, fullscreen, left drawer and right drawer.

Frequently asked questions

Why does my YouTube video keep playing after I close the popup?

Because the popup was hidden without anything telling the player to stop. The YouTube player runs in an iframe and has to be reloaded or sent a pause command. Divi 5’s Hide Element interaction does this for you; popups hidden with CSS or a class, and some popup plugins, do not.

Does Escape close a popup while a video has focus?

Usually. One exception: while keyboard focus is inside Chrome’s built-in video controls, Chrome does not pass Escape to the page. Press Tab to reach the close button instead.

Will autoplay work in a popup?

Browsers only allow autoplay reliably for muted video. If the video should play with sound, let the visitor press play inside the popup.

Does a video in a hidden popup slow the page down?

It can, if the embed loads with the page. Lazy loading, which Divi Smart Popup applies automatically, means it loads only when the popup opens.

Did this answer the question? No — open a support request