Image popup
A full-size image viewer on the native dialog element: Escape closes it, focus returns to the thumbnail, and the large image keeps its alt text.
CL-0036 fixesChecked 2026-10-151. The prompt
Not yet run against a named assistant · 113 words · CC-BY-4.0Write an image popup in plain JavaScript using the native <dialog> element and showModal(). Each thumbnail is a link with the class popup that points to the full-size image, so it still opens where scripts or dialog support are missing. Use one click listener on the document. Set the large image's src and alt as properties, never by writing HTML, and take the alt from a data-alt attribute or the thumbnail's alt text. Put autofocus on a visible Close button, close the dialog when the backdrop is clicked, leave Escape to the browser, clear the image source on close and return focus to the thumbnail that opened it. Add an MIT licence comment.
What a one-line prompt gets you
Representative output for “an image popup lightbox in JavaScript” and nothing else, written by the editors to show the pattern. It runs. It also leaves these gaps.
- 1 Popup blockers can stop it.window.open runs in the image onload handler, after the click. MDN says popups must be opened in direct response to user input, and that window.open returns null when a blocker stops it, so the next line throws. The reviewed code opens a dialog inside the click handler.Prompt line: “Using the native <dialog> element and showModal().”
- 2 The link goes nowhere without JavaScript.MDN warns that javascript: hrefs misbehave when copied, opened in a new tab, bookmarked or loaded with scripts off, and give screen readers the wrong semantics. The reviewed thumbnail is a real link to the image.Prompt line: “Each thumbnail is a link with the class popup that points to the full-size image.”
- 3 Neither image has alt text.A screen reader user learns nothing about the picture they asked to see. The reviewed code copies data-alt, or the thumbnail's alt, to the large image.Prompt line: “Take the alt from a data-alt attribute or the thumbnail's alt text.”
- 4 Focus ends up in another window.The picture opens in a separate window, which MDN's accessibility notes call disorienting because a popup has no Previous button. The reviewed dialog stays in the page and sends focus back to the thumbnail when it closes.Prompt line: “Return focus to the thumbnail that opened it.”
- 5 It builds HTML from a string.document.write with a concatenated src turns a crafted file name into markup. The reviewed code sets src and alt as properties, so nothing is parsed as HTML.Prompt line: “Set the large image's src and alt as properties, never by writing HTML.”
- 6 It carries no licence.Nothing says who wrote it or on what terms. The reviewed script starts with an MIT licence line and a credit.Prompt line: “Add an MIT licence comment.”
Install it
Questions and answers
Has this prompt been tested with an assistant?
Not yet. The editors wrote the prompt to describe the reviewed code, which they wrote and tested with AI assistance and re-ran in a scripted DOM test on 2026-10-15. Nobody has run the prompt against a named assistant, so compare any answer with the code above.
Does the popup close with the Escape key?
Yes, without extra script. MDN's dialog reference says modal dialogs can be closed by pressing Esc, and that a dialog opened with showModal() and no closedby attribute behaves as if closedby were set to closerequest.
How does a click outside the image close it?
A click on the dark backdrop lands on the dialog element itself, while a click on the picture lands on the img. The script closes the dialog only when the event target is the dialog.
Where should focus go when the popup closes?
Back to the thumbnail that opened it. The WAI-ARIA Authoring Practices dialog (modal) pattern says focus returns to the invoking element when a dialog closes, unless that element no longer exists.
Why is autofocus on the Close button?
MDN recommends autofocus on the element the reader will use first, and suggests the close button when nothing else needs immediate interaction. The APG pattern also strongly recommends a visible button that closes the dialog.
Why not put tabindex on the dialog?
MDN's dialog page warns that the tabindex attribute must not be used on the dialog element. Focus belongs on the controls inside it.
Why not open the image with window.open?
MDN says modern browsers have strict popup blocker policies, that window.open returns null when blocked, and that it opens a new tab by default rather than a sized window. A dialog keeps the picture in the page.
Is the dialog element supported everywhere?
MDN lists the dialog element and showModal() as Baseline widely available, across browsers since March 2022 (checked 2026-10-15). Where showModal is missing, the script steps aside and the link opens the image directly.
Can I use the image popup on a client site?
Yes. The code is MIT licensed, so you can use and change it in paid work; keep the licence line or credit CodeLifter in your notices.
