Skip to content

ES6 - Dialog Boxes

JavaScript, when running in a web browser, can utilize built-in dialog boxes to interact with the user. These include alert(), confirm(), and prompt(). While they are simple to use, modern web applications often prefer custom modal dialogs for better user experience and styling control, as these native dialogs are blocking and can disrupt the user flow.

The alert() method displays a modal dialog with a specified message and an OK button. It’s often used for simple notifications or warnings.

HTML Structure:
```html
<button id="showAlertBtn">Show Alert</button>
<p id="alertResult"></p>

JavaScript Code:

function showAlert() {
alert("This is an alert message!");
// Code here executes after the user clicks OK.
document.getElementById('alertResult').textContent = 'Alert was shown and dismissed.';
}
// It's better to attach event listeners using JavaScript
document.getElementById('showAlertBtn').addEventListener('click', showAlert);
When the "Show Alert" button is clicked, a dialog box will appear with the text "This is an alert message!" and an "OK" button. The script execution pauses until the user dismisses the alert. After dismissal, the paragraph with id `alertResult` will be updated.
## Confirmation Dialog Box
The `confirm()` method displays a dialog box with a specified message and two buttons: OK and Cancel. It's used to get a boolean confirmation from the user.
`confirm()` returns `true` if the user clicks OK, and `false` if the user clicks Cancel or closes the dialog.
### Example:
```css
HTML Structure:
```html
<button id="showConfirmBtn">Show Confirmation</button>
<p id="confirmResult"></p>

JavaScript Code:

function getConfirmation() {
const userResponse = confirm("Do you want to proceed?");
const resultParagraph = document.getElementById('confirmResult');
if (userResponse) {
resultParagraph.textContent = "User chose to proceed (OK).";
} else {
resultParagraph.textContent = "User chose not to proceed (Cancel).";
}
}
document.getElementById('showConfirmBtn').addEventListener('click', getConfirmation);
Clicking the "Show Confirmation" button will display a dialog asking "Do you want to proceed?" with "OK" and "Cancel" buttons. The text in the `confirmResult` paragraph will update based on the user's choice.
## Prompt Dialog Box
The `prompt()` method displays a dialog box that prompts the user for input. It includes a message, an input field for text, and OK/Cancel buttons.
`prompt()` takes two optional arguments: `message` (the text to display to the user) and `defaultText` (a string to display in the input field by default). It returns the string entered by the user if they click OK, or `null` if they click Cancel or close the dialog. If the user clicks OK without entering text, it returns an empty string.
### Example:
```css
HTML Structure:
```html
<button id="showPromptBtn">Get User Input</button>
<p id="promptResult"></p>

JavaScript Code:

function getUserInput() {
const userName = prompt("Please enter your name:", "Guest User");
const resultParagraph = document.getElementById('promptResult');
if (userName === null) {
resultParagraph.textContent = "User cancelled the prompt.";
} else if (userName === "") {
resultParagraph.textContent = "User entered an empty name.";
} else {
resultParagraph.textContent = `Welcome, ${userName}!`;
}
}
document.getElementById('showPromptBtn').addEventListener('click', getUserInput);
Clicking "Get User Input" will show a dialog asking for the user's name, with "Guest User" as the default text in the input field. The `promptResult` paragraph will display a message based on what the user enters or if they cancel.
### Considerations for Modern Web Development:
- User Experience: Native dialogs are synchronous (blocking), meaning they halt script execution and can feel intrusive. Modern UIs often use asynchronous, non-blocking custom modals or notifications that integrate better with the page.
- Styling: Native dialogs cannot be styled with CSS, leading to inconsistencies across browsers and platforms.
- Functionality: They offer limited functionality compared to custom solutions built with HTML, CSS, and JavaScript.
- Accessibility: While browser vendors aim for accessible native dialogs, custom solutions offer more control to ensure full ARIA compliance and keyboard navigation.
For creating richer, more integrated user interactions, explore building custom UI components or using libraries like Bootstrap Modals, Material UI Dialogs, or similar.