JavaScript: Overriding alert()
It's definitely "supported". It is your web page, you do whatever you want to with it.
I already did this to track analytics events without modifying a library but by sneaking into events.
Use the proxy pattern:
(function(proxied) {
window.alert = function() {
// do something here
return proxied.apply(this, arguments);
};
})(window.alert);
You can also bypass the call to the original function if you want (proxied)
More info here: JQuery Types #Proxy Pattern
Although most browsers support overriding it, be careful with what you're doing with it.
Since the default alert box blocks the execution thread, some libraries that rely on this behaviour might not work anymore (at best).
You should be a good citizen and avoid touching the native API. If you do, you could break things up, when using 3rd party code.
Yet, if you want to redefine the alert behaviour in a specific context, you could enclose it with an anonymous function, like this:
/* new funky alert */
function myFunkyAlert(msg) {
/* here goes your funky alert implementation */
alert("Look ma!\n" + msg);
}
(function(alert) { // anonymous function redefining the "alert"
/* sample code */
alert("Hello World!");
})(myFunkyAlert);
There are no dangers in Overring alert function. Every browser supprts it.
for example:
// function over riding. Redirecting to Console with Firebug installed.
function alert(message) {
console.info(message);
}
alert('This is an override.');