all topics

Browser Interaction

alert, prompt, confirm

Three built-in browser functions for the simplest possible interaction with a visitor, no custom UI required.

js
alert("Message");
 
let result = prompt("Your question?", "default text");
// result is the text entered, or null on Cancel/Esc
 
let ok = confirm("Are you sure?");
// ok is true on OK, false on Cancel/Esc
  • alert just shows a message. Nothing to return.
  • prompt shows a message and a text field. Returns whatever the visitor typed, or null specifically if they hit Cancel or Esc instead.
  • confirm shows a message with OK/Cancel. Returns true for OK, false for Cancel or Esc.

They're all modal

Every one of these pauses script execution, and the rest of the page stays completely unresponsive until the visitor dismisses the window. Nothing else on the page can run or be interacted with while one's open.

Two things you can't control

  • Where the window appears is entirely up to the browser, usually centered, but not something the page gets to decide.
  • What the window actually looks like is also the browser's call, not stylable from the page's own CSS.

That's the trade for how simple they are to use. When the look and position genuinely matter, something richer is the right tool, these three are for when they don't.