Home Videos Exercises MCQ Q&A Quiz E-Store Services Blog Sign in Appointment Payment

JavaScript Popup

JavaScript provides built-in global functions to display popup message boxes for different purposes.

A JavaScript popup can be created using various methods depending on the desired style or interaction.


JavaScript Message Boxes: alert(), confirm(), prompt()

  • alert(message): Display a popup box with the specified message with the OK button.
  • confirm(message): Display a popup box with the specified message with OK and Cancel buttons.
  • prompt(message, defaultValue): Display a popup box to take the user's input with the OK and Cancel buttons.

Note: In JavaScript, global functions can be accessed using the window object like window.alert(), window.confirm(), window.prompt().


alert()

The alert() function displays a message to the user to display some information to users. This alert box will have the OK button to close the alert box.

The alert() function takes a paramter of any type e.g., string, number, boolean etc. So, no need to convert a non-string type to a string type.

Example alert():
alert("Welcome to soopro pathshala"); // display string message
alert('This is a numer: ' + 200); // display result of a concatenation
alert(200); // display number
alert(Date()); // display current date


Confirm()

Use the confirm() function to take the user's confirmation before starting some task. For example, you want to take the user's confirmation before saving, updating or deleting data.

The confirm() function displays a popup message to the user with two buttons, OK and Cancel. The confirm() function returns true if a user has clicked on the OK button or returns false if clicked on the Cancel button. You can use the return value to process further.

The following takes user's confirmation before saving data: Example confirm():
var userPreference;
if (confirm("Do you want to save changes?") == true) {
userPreference = "Data saved successfully!";
} else {
userPreference = "Save Cancelled!";
}


Prompt()

Use the prompt() function to take the user's input to do further actions. For example, use the prompt() function in the scenario where you want to calculate EMI based on the user's preferred loan tenure.

The prompt() function takes two parameters. The first parameter is the message to be displayed, and the second parameter is the default value in an input box.

Example prompt():
var name = prompt("Enter your name:", "Lovely");
if (name == null || name == "") {
document.getElementById("msg").innerHTML = "You did not entert anything. Please enter your name again";
} else
{ document.getElementById("msg").innerHTML = "You enterted: " + name;
}


Key points of JS popup

1. Types of Popups:

  • Alert: Displays a message with an "OK" button, often used for simple notifications.
  • Confirm: Displays a message with "OK" and "Cancel" buttons, returning true for "OK" and false for "Cancel".
  • Prompt: Allows user input with "OK" and "Cancel" buttons, returning the input or null.

2. Uses Context:

  • Use alert for quick notifications.
  • Use confirm for confirmation actions, like deleting a file.
  • Use prompt to capture user input, though for better UX, custom input dialogs are often preferable.

3. Custom Popups:

  • You can create custom popups with HTML, CSS, and JavaScript for more styling control and functionality.
  • Custom popups can include additional elements like images, forms, and interactive content.

4. Event-Driven Display:

  • Popups can be shown based on events, like button clicks, page load, or other user interactions.
  • Attach JavaScript functions to handle these events and control the visibility of custom popups.

5. Accessibility & User Experience:

  • Avoid excessive use of popups, as they can interrupt the user experience.
  • Ensure popups are accessible by focusing on readability, appropriate colors, and keyboard navigation.

6. Security Considerations:

  • Avoid using popups for sensitive information or authentication; use secure, dedicated interfaces instead.

7. Modern Alternatives:

  • Modals (custom popups styled with CSS/JavaScript) are more flexible than traditional popups.
  • Libraries like Bootstrap or frameworks like React provide built-in modal components for easier integration and styling.