It's not possible. The function is composed of native code:
console.log(window.alert.toString());
From an answer on a similar topic:
[Native code] means that it's not an ordinary Javascript function. Functions composed of native code are always functions provided by the browser (or whatever environment the code is running in); they often cannot be emulated by any Javascript functions you write yourself. For example, only the window.history
object can perform history actions like history.back()
; if window.history
gets overwritten, and you don't have a saved reference to it or any other history
objects, there's no way to write your own function that can do history.back()
, because .back()
invokes privileged native code that requires an interface between the visible Javascript and the browser engine.
Many functions made from native code are not implementable in plain Javascript, including alert
, because it uses privileged browser-provided functions. Unless all you want to do is alter the text that gets displayed in the alert box (or if the alert appears at all), perhaps by monkeypatching window.alert
, no further customization is possible.
That said, it's good to avoid alert
anyway in most cases, because it's pretty user-unfriendly. Better to construct a proper modal that doesn't block.