How do you replace an old popup window script with a dialog?
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.
Cite
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.- CodeLifter editorsStaffStaff, CodeLifter ·
What did the old popup script do?
The classic pattern was a small function and a link that called it:
<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, read on 16 October 2026, says popup windows "must be opened in direct response to user input", and that the method returns
nullwhen 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 thejavascript: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 withshowModal()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 tabNo script needed An ad or anything that opened on page load Nothing Browsers block it anyway 
What each kind of old popup should become, per this article Build the dialog replacement
- Move the content into the page. Copy the body of
shipping.htmlinto a<dialog>element near the end of the page. Keepshipping.htmltoo, as the fallback. - Make the link real. Point
hrefat the page itself, so the link works with scripts off, in a new tab and as a bookmark. - Add a close button that needs no script. MDN's dialog reference says a
<form>inside the dialog withmethod="dialog"closes it when submitted. Putautofocuson that button; MDN recommends it on the close button when nothing else needs immediate interaction. - 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.
<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/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: 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
tabindexto the<dialog>; MDN says it "must not be used" there. The image popup prompt 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 andcommand="close"closes it, with no script. MDN's Invoker Commands API page 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 withcommandfordoes nothing in a browser that lacks it, so for now the link plusshowModal()above is the safer default; use invoker commands where you control which browsers your visitors use.Check it before you ship
- Click the link, then press Esc. The dialog should close and focus should return to the link. The W3C's modal dialog pattern says focus returns to the element that opened the dialog unless it no longer exists.
- Tab through the open dialog. Focus should never land on the page behind it.
- Open the link in a new tab, and with JavaScript turned off. Both should load
shipping.html. - Search the project for
window.openandjavascript: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. The modernising thread covers the rest of an old script, and the Script Shelf 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 withshowModal(); full pages stay normal links. - Keep a real
hrefas the fallback and only cancel it whenshowModalexists. - Close with
<form method="dialog">and Esc; putautofocuson the close button. - Invoker commands (
command="show-modal") need no script but are newer; check your audience first.
- Move the content into the page. Copy the body of
Write something first.
Give people something to work with: at least 30 words on what happened and what you tried.
That is too long. Keep it under 6,000 characters.
Write the question as the title, 15 to 140 characters, no links.
Pick a category.
Add a name (2 to 40 characters, no links).
That email address does not look right.
That was quick. Read the thread, then try again.
The form expired. Reload the page and post again.
Something went wrong with the form. Reload and try again.
Please complete the check and post again.
Limit reached for now. Try again later.
This thread is closed to new replies.
This thread no longer accepts replies.
Something went wrong with the form. Reload and try again.
Post a reply or question first (name and email), then this browser can vote, edit and accept answers.
You cannot vote on your own post.
Only the author (within 30 days) or the forum team can do that.
That email already has an account. Use your sign-in link instead.
New accounts are paused for the moment. Try again later.
That was already posted.
1 answer
- CodeLifter editorsStaffStaff, CodeLifter ·
Is a popover the same as a dialog? No. They overlap, but a popover is always non-modal. MDN's Popover API page 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
popoverattribute to an element and apopovertargetattribute to the button that toggles it, with no script. The browser shows it by adding it to the top layer, and in the defaultautostate 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, becauseshowModal()makes the page behind it inert.The two combine: MDN's dialog reference shows
<dialog popover>controlled bypopovertarget, which gives dialog semantics with popover behaviour. It is non-modal, so clicking outside closes it.