roosteronacid roosteronacid - 4 years ago 233
Javascript Question

JavaScript: Overriding alert()

Has anyone got any experience with overriding the

alert()
function in JavaScript?


  • Which browsers support this?

  • Which browser-versions support this?

  • What are the dangers in overriding the function?


Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download