JS Browser Interactions : alert, prompt, and confirm #4

Hi Friends!

Kamon acho!

JavaScript provides simple methods to interact with users through modal dialogs. These built-in functions—alert, prompt, and confirm—allow you to display messages, collect input, and get confirmations, respectively. In this post, we explore these functions in detail with plenty of code examples.

What Is a Modal Window?

A modal window is a popup that interrupts the user’s flow by disabling interaction with the rest of the page until it is dismissed. For example, when you run this code:

alert("This popup is modal: You must click OK before continuing!");
console.log("This line will not execute until the alert is dismissed.");

The alert forces the user to click “OK” before the script continues, making it “modal.”

The alert Function

The simplest interaction is the alert function. It shows a message in a popup and halts script execution until the user clicks “OK.”

alert("Hello, world!");
console.log("This line runs after the alert is closed.");

This is useful for displaying important notifications or error messages. Notice how the message is clear, and no further action is taken until the popup is dismissed.

Collecting User Input with prompt

When you need to collect data from the user, the prompt function comes in handy. It displays a modal window with an input field. The syntax is:

let userName = prompt("What is your name?", "Guest");
if (userName !== null) {
  console.log(`User entered: ${userName}`);
} else {
  console.log("User canceled the prompt.");
}

In this snippet, if the user enters a value and presses OK, it is stored in userName. If they cancel, the value is null.
Note: In Internet Explorer, if you omit the second parameter, it may default to displaying "undefined". Always provide a default value (even an empty string) to avoid that:

let testValue = prompt("Enter something:", ""); // Avoids 'undefined' in IE

Confirming Actions with confirm

For simple yes/no decisions, the confirm function is ideal. It shows a dialog with “OK” and “Cancel” buttons:

let isProceed = confirm("Do you want to proceed?");
if (isProceed) {
  console.log("User chose OK.");
} else {
  console.log("User chose Cancel.");
}

Here, confirm returns true when OK is pressed and false when Cancel is pressed. This allows you to execute code conditionally based on user confirmation.

Limitations of These

While these functions are very handy, they come with a couple of limitations:

  1. Fixed Location:
    The browser determines where the modal dialog appears on the screen (usually centered). For example, you cannot specify:

     // The position of this alert is controlled by the browser.
     alert("This alert's position is fixed by the browser.");
    
  2. No Custom Styling:
    Their appearance is controlled by the browser and cannot be modified using CSS or JavaScript.

     // Attempting to add custom styles to alert, prompt, or confirm is not possible.
     alert("I cannot be styled with CSS!");
    

Wrapping Up

JavaScript’s alert, prompt, and confirm are excellent for quick, simple user interactions. However, for more advanced interfaces, consider building custom modals with HTML, CSS, and JavaScript. Experiment with these functions to understand their blocking behavior and limitations:

// Example combining all three functions:
alert("Welcome to our site!");

let userName = prompt("Enter your name:", "Guest");
if (userName !== null) {
  let isConfirmed = confirm(`Is "${userName}" correct?`);
  if (isConfirmed) {
    alert(`Welcome, ${userName}!`);
  } else {
    alert("Please try again.");
  }
}

Happy coding, and enjoy creating interactive web pages!