views:

1547

answers:

9

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?
+1  A: 

It sure works in firefox and ie8. I can't see that there'd be any browser it wouldn't work in. This is pretty much fundamental of how javascript works, even though one don't often see it used with native functions like that =)

David Hedlund
The Browser to target specifically would be IE6 others would likely be ok with it.
AnthonyWJones
+8  A: 

I think every Javascript implementation will support this, and there is no danger involved with it. It's commonly done to replace the plain OS-styled alert boxes to something more elegant with HTML/CSS. Doing it this way means you don't have to change existing code! The fact that it is possible makes Javascript awesome.

Josh Stodola
+1  A: 

All JavaScript implementations in modern browsers support overriding.

The dangers are quite simply, that you would drive other team members absolutely crazy by overriding commonly known functions such as alert().

So unless you are overriding functions as a tool to perhaps debug or hack existing code in some way, I don't see any reason to do it. Just create a new function.

Chris Tek
+1  A: 

When it comes to js browser functions window.alert is pre-eminent and most well known, people who don't know js know alert() -- rest assured it is supported in all browsers in use today and your code snippet is as well. However, I wouldn't override (well this is more like refactoring rather than override in the OOP sense) alert() for a particular use case as yours because when you actually need to use alert() with no template, and you probably will, then you'll need another non-alert function to do so.

non sequitor
+14  A: 

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() {
  var proxied = window.alert;
  window.alert = function() {
    // do something here
    return proxied.apply(this, arguments);
  };
})();

You can also bypass the call to the original function if you want

More info here: http://docs.jquery.com/Types#Proxy%5FPattern

Mike Gleason jr Couturier
Superb answer! I completely forgot abut that pattern :)
roosteronacid
No problem! I also share Josh Stodola's view on this, great comment too..
Mike Gleason jr Couturier
Aye. Already up-voted his answer. Yours is more explicit thou.. And it comes with an example. Thats always a crowd-pleaser :)
roosteronacid
+1 on the proxy pattern to *truly* override the func and ensure it doesn't get tampered with!
Josh Stodola
Ugh! `apply()` is not available on `window.alert` in Internet Explorer 8.
roosteronacid
I'm sorry to hear that.. `apply` is a method of the `Function` object. So they must explicitly restricted it for `alert`?!
Mike Gleason jr Couturier
They must've overridden it using the proxy pattern :D
Josh Stodola
lol.. you faced a typical MS "this behavior is by design" :) OTOH Does this answer answers your question? Yes I'm a whore :)
Mike Gleason jr Couturier
+2  A: 

My experience with overriding alert() function is that we once used it to "hack" trial version of JavaScript library that displayed "Please register!" nag screen through alert time to time.

We just defined our own alert() function and voila.

It was for testing purposes only, we bought full version later, so nothing immoral going on here ;-)

Josef Sábl
+1  A: 

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.')

+1  A: 

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);
Pablo Cabrera
A: 

So how can I redefine it for IE8?

Ladislav
Redefine what? This should work fine in IE8
Pekka