Skip to main content

alert

Thin wrappers around the browser's alert, confirm, and prompt dialogs.

Import: @rtorcato/browser-common/alert

๐Ÿ“– MDN: Window.alert()

showAlert, showConfirm, and showPrompt all throw a requires a browser environment error when called outside a browser (no window) โ€” there is no feature-check function, so guard with your own environment check if you might run this code server-side.

Browser supportโ€‹

APIChromeEdgeFirefoxSafariiOS SafariBaseline
Window.alertโœ… 1+ [1]โœ… 12+โœ… 1+โœ… 1+โœ… 1+Widely available
Window.confirmโœ… 1+ [1]โœ… 12+โœ… 1+โœ… 1+โœ… 1+Widely available
Window.promptโœ… 1+ [1]โœ… 12+โœ… 1+ [2]โœ… 1+โœ… 1+Widely available
  1. Chrome: Starting with Chrome 46, this method is blocked inside an <iframe> unless its sandbox attribute has the value allow-modals.
  2. Firefox: Firefox strips newline characters from the prompt response; see bug 1716229.

โœ… supported ยท โ— partial ยท โŒ not supported ยท [n] see note. Generated from MDN browser-compat-data and web-features. caniuse: Window.alert ยท Window.confirm ยท Window.prompt

Exampleโ€‹

import { showAlert, showConfirm, showPrompt } from '@rtorcato/browser-common/alert'

showAlert('Saved!')

if (showConfirm('Delete item?')) {
deleteItem()
}

const name = showPrompt('Your name?', 'Anon')

Exportsโ€‹

  • showAlert(message) โ€” shows an alert dialog
  • showConfirm(message) โ€” shows a confirm dialog, returns true if OK was clicked
  • showPrompt(message, defaultValue?) โ€” shows a prompt dialog, returns the input or null

See the API reference for full signatures.