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โ
| API | Chrome | Edge | Firefox | Safari | iOS Safari | Baseline |
|---|---|---|---|---|---|---|
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 |
- Chrome: Starting with Chrome 46, this method is blocked inside an <iframe> unless its sandbox attribute has the value allow-modals.
- 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 dialogshowConfirm(message)โ shows a confirm dialog, returnstrueif OK was clickedshowPrompt(message, defaultValue?)โ shows a prompt dialog, returns the input ornull
See the API reference for full signatures.