3

I have done something like this a long time ago where I captured the alerts and prevented the default browser based alert box from popping up and replaced it with a modal of one kind or another. However its been so long since I have done that, and I can't find any reference from old code of mine how I did it, nor can I find anything relevant via google currently. So.. I am hoping someone here can aid me in this and help me out. I havent tried anything yet so save yourself the question of what did I try. Other than spending the last hour or so googling different phrases for any snipplet of code that resembles whats in my blurry memory I've come up empty handed. I know, this is kind of a poor quality question to, but at the same time I am sure others would appreciate knowing the answer as well.

All I want to do in my case is capture the event that would trigger the alert() box and pass the message that was in it to another variation of notification. Currently I am doing some work in appmobi with a couple others and I want to take alert() capture it then use

AppMobi.notification.alert(message,title,buttontext); as the default action for alert()

chris
  • 36,115
  • 52
  • 143
  • 252
  • possible duplicate of [JavaScript: Overriding alert()](http://stackoverflow.com/questions/1729501/javascript-overriding-alert) – Oleg Sep 11 '12 at 08:46

3 Answers3

11

You can simply overwrite the alert method:

window.alert = function(msg) {
   // whatever you want to do with 'msg' here
};

Note that this won't have the blocking behavior of a regular alert

David Hedlund
  • 128,221
  • 31
  • 203
  • 222
  • `window` thats what it was.. I knew it was something to that effect damn, talk about thinking in the wrong direction. Very cool, thank you. – chris Sep 11 '12 at 06:40
  • 2
    @chris: well, you can set your own value to `alert` in whichever scope you wish for. `window` denotes global scope. you could set it in a closure, to overwrite the meaning of the alert only in that closure. – David Hedlund Sep 11 '12 at 06:42
  • +1 for mentioning the blocking/synchronous behaviour of a standard `alert()`. – nnnnnn Sep 11 '12 at 06:49
1
window.alert = function(message,title,buttontext) {
   AppMobi.notification.alert(message,title,buttontext); 
};
Diode
  • 24,570
  • 8
  • 40
  • 51
1

As others have pointed out, it can be overridden - just remember that AppMobi.notification.alert expects three arguments. I would not be surprised if it has fallback defaults, but better safe than sorry:

//alert is a native code function that we may need elsewhere
window.legacyAlert = window.alert;
window.alert = function(_msg) {
  AppMobi.notification.alert(_msg, 'Alert', 'OK');//or whatever
};

This overrides the method globally. Alternatively you may require for it to be overridden only in some parts of your code using closures:

alert('this is a vanilla js alert');
(function(alert) {
    alert('this alert has been overridden');
})(AppMobi.notification.alert);
alert('this is another vanilla js alert');

Fiddled

Oleg
  • 24,465
  • 8
  • 61
  • 91