Stop a video when its dialog closes

Updated: November 26, 2026

If closing a video window removes the picture but the audio keeps playing, pause playback on close. For your own HTML dialog, listen for close and call pause on the video element. For an embedded hosted player, use its documented pause interface or remove the iframe when you close it.

Identify how the player is mounted

On a product page, “Watch the demo” might open a dialog containing a video element or an iframe. With the video element, your page controls the file and the player. A cross-origin iframe contains a separate document. Hiding either one with styles alone does not guarantee that playback will stop.

Tie the pause to every close path

For a native HTML dialog, give it the ID demo-dialog and run the example after the document has loaded. Put the video element inside that dialog. The handler runs when the button or Escape closes the dialog, where Escape is supported. It can leave the playback position alone. A different modal component may document a different close event.

If the dialog contains several media elements, make closing it pause each one that can keep playing. Also check that focus does not jump to an unrelated link in the page behind it. When the dialog opens again, let the viewer choose whether to resume.

const dialog = document.getElementById("demo-dialog");
const clip = dialog.querySelector("video");
dialog.addEventListener("close", () => clip.pause());

Test audio and keyboard interaction

Start the demo, listen for a moment, and close it with the button. The audio should stop. Repeat with Escape and check where focus returns. If you remove an iframe, recreate it the next time the dialog opens and expect that the position may be lost. Check that opening again does not create a second player.

Frequently asked questions

For a video element, pause keeps its current time. For an iframe, use any options the provider documents.

You might also like

Host your videos for free and earn from every view

Create your account in under a minute, upload your first video and start earning from day one.