Replace alert() with SweetAlert

Posted by Joe Enos on March 09, 2021 · 1 min read

The default alert() functionality in browsers has improved over the years, but in general, it's still an ugly alert box. One library that displays nice looking alerts is SweetAlert, which is a very nice looking alert or dialog box, with lots of customization options and styling.

If you have an existing codebase where you're using the default alert often, and don't want to touch your existing code, you can use this technique to overwrite the global alert function with a call to SweetAlert (or any other message box library of your choice). You won't get all the custom stuff, but you'll get a nicer message than the standard one.

SweetAlert continues to execute script after showing the popup, so if any of your code relies on the alert blocking script execution, then you won't be able to use this technique.

View code on GitHub