# How do you replace an old popup window script with a dialog?

Source: https://codelifter.com/forum/posts/how-do-you-replace-an-old-popup-window-script-with-a-dialog/
Site: CodeLifter Forum, category Script Shelf
Published: 2026-11-30. Updated: 2026-12-01. Replies: 1.

## Question by CodeLifter editors (staff), 2026-11-30

To replace an old `window.open()` popup, put the content in a `<dialog>` element on the same page and open it with `showModal()` from a click on a real link or button. Close it with a `<form method="dialog">` button or Esc. Keep the link's `href` pointing at the content, so it still works if the script fails.

## What did the old popup script do?

The classic pattern was a small function and a link that called it:

```html
<a href="javascript:popUp('shipping.html')">Shipping details</a>
<script>
function popUp(url) {
  window.open(url, "info", "width=400,height=300,toolbar=no,scrollbars=yes");
}
</script>
```

That is an example written for this answer, but every part of it is now a problem. MDN's [window.open() reference](https://developer.mozilla.org/en-US/docs/Web/API/Window/open), read on 16 October 2026, says popup windows "must be opened in direct response to user input", and that the method returns `null` when a popup blocker stops it. Size and toolbar settings are legacy features that "only have the effect of requesting a popup", so the browser, not the script, decides what the window looks like. And the `javascript:` link causes "unexpected behavior when copying/dragging links, opening links in a new tab/window, bookmarking, or when JavaScript is loading, errors, or is disabled."

MDN's accessibility notes add the user's side: people prefer tabs, and "in the case of a popup, no toolbar provides a 'Previous' button" to get back.

## Dialog, new tab, or plain link?

Look at what the popup showed before you pick a replacement.

| The popup showed | Replace with | Why |
| --- | --- | --- |
| A short message, help text, an enlarged image or a small form | `<dialog>` opened with `showModal()` | Stays in the page; no blocker; Esc closes it |
| A full page someone might bookmark or read at length | A normal link | The visitor chooses tab or window themselves |
| An external site you do not want to replace your page | A link with `target="_blank"`, labelled as opening a new tab | No script needed |
| An ad or anything that opened on page load | Nothing | Browsers block it anyway |

![Table matching four kinds of old popup content to a dialog, a normal link, a new-tab link or nothing](/forum/img/posts/what-to-replace-an-old-popup-window-with.webp "What each kind of old popup should become, per this article")

## Build the dialog replacement

1. **Move the content into the page.** Copy the body of `shipping.html` into a `<dialog>` element near the end of the page. Keep `shipping.html` too, as the fallback.
2. **Make the link real.** Point `href` at the page itself, so the link works with scripts off, in a new tab and as a bookmark.
3. **Add a close button that needs no script.** MDN's [dialog reference](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/dialog) says a `<form>` inside the dialog with `method="dialog"` closes it when submitted. Put `autofocus` on that button; MDN recommends it on the close button when nothing else needs immediate interaction.
4. **Open it with `showModal()` from the click.** Cancel the link's default action only when the method exists, so older browsers follow the link instead.

```html
<a href="/shipping.html" id="shipping-link">Shipping details</a>

<dialog id="shipping">
  <h2>Shipping details</h2>
  <p>Orders leave within two working days.</p>
  <form method="dialog">
    <button autofocus>Close</button>
  </form>
</dialog>

<script src="/js/shipping.js" defer></script>
```

```js
// js/shipping.js
const link = document.getElementById("shipping-link");
const dialog = document.getElementById("shipping");

if (link && dialog && typeof dialog.showModal === "function") {
  link.addEventListener("click", (event) => {
    event.preventDefault();
    dialog.showModal();
  });
}
```

What you get from the browser, per MDN's [showModal() page](https://developer.mozilla.org/en-US/docs/Web/API/HTMLDialogElement/showModal): the dialog sits in the top layer with a `::backdrop`, and the rest of the page becomes inert, so it cannot be clicked or focused until the dialog closes. Modal dialogs "can also be closed by pressing the Esc key." Both the element and the method have worked across browsers since March 2022.

Do not add `tabindex` to the `<dialog>`; MDN says it "must not be used" there. The [image popup prompt](https://codelifter.com/prompts/image-popup/) shows the same pattern for enlarged pictures, with a tested script that clears the image and returns focus on close.

## Can you open it with no JavaScript at all?

In current browsers, yes. MDN's dialog page documents invoker commands: `<button command="show-modal" commandfor="shipping">` opens the dialog and `command="close"` closes it, with no script. MDN's [Invoker Commands API page](https://developer.mozilla.org/en-US/docs/Web/API/Invoker_Commands_API) labels it Baseline 2025, working across the latest browsers since December 2025, and warns it "might not work in older devices or browsers". A button with `commandfor` does nothing in a browser that lacks it, so for now the link plus `showModal()` above is the safer default; use invoker commands where you control which browsers your visitors use.

## Check it before you ship

1. Click the link, then press Esc. The dialog should close and focus should return to the link. The W3C's [modal dialog pattern](https://www.w3.org/WAI/ARIA/apg/patterns/dialog-modal/) says focus returns to the element that opened the dialog unless it no longer exists.
2. Tab through the open dialog. Focus should never land on the page behind it.
3. Open the link in a new tab, and with JavaScript turned off. Both should load `shipping.html`.
4. Search the project for `window.open` and `javascript:` to find the other popups: `grep -rn "window\.open\|javascript:" --include="*.html" --include="*.js" .`

For trapping focus and choosing the first focused control, see the follow-ups on the [keyboard prompting thread](/forum/posts/how-do-i-prompt-for-javascript-that-works-with-a-keyboard/). The [modernising thread](/forum/posts/how-do-you-modernise-an-old-cut-and-paste-javascript-script/) covers the rest of an old script, and the [Script Shelf](https://codelifter.com/scripts/) lists the popup scripts CodeLifter retired.

## Short version

- Old popups break on blockers, ignored window settings and `javascript:` links.
- Short content goes in a `<dialog>` opened with `showModal()`; full pages stay normal links.
- Keep a real `href` as the fallback and only cancel it when `showModal` exists.
- Close with `<form method="dialog">` and Esc; put `autofocus` on the close button.
- Invoker commands (`command="show-modal"`) need no script but are newer; check your audience first.

## Reply 1 by CodeLifter editors (staff), 2026-12-01

**Is a popover the same as a dialog?** No. They overlap, but a popover is always non-modal. MDN's [Popover API page](https://developer.mozilla.org/en-US/docs/Web/API/Popover_API) says "Popovers created using the Popover API are always non-modal", meaning the rest of the page stays usable while it is open, and adds: "If you want to create a modal popover, a `<dialog>` element is the right way to go." The same page labels the Popover API Baseline 2025, working across the latest browsers since January 2025.

A popover suits small, light content such as a menu, a tooltip-like note or a "copied" message. You add the `popover` attribute to an element and a `popovertarget` attribute to the button that toggles it, with no script. The browser shows it by adding it to the top layer, and in the default `auto` state a click outside closes it, which MDN's dialog page calls light dismiss. A modal `<dialog>` suits content that needs an answer or full attention, such as a form, a confirmation or an enlarged image, because `showModal()` makes the page behind it inert.

The two combine: MDN's [dialog reference](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/dialog) shows `<dialog popover>` controlled by `popovertarget`, which gives dialog semantics with popover behaviour. It is non-modal, so clicking outside closes it.

---
Cite as: CodeLifter Forum, "How do you replace an old popup window script with a dialog?", https://codelifter.com/forum/posts/how-do-you-replace-an-old-popup-window-script-with-a-dialog/, accessed 2026-12-01.
