4

Lets say we have alert method of window object. I would like to enhance it with nice alertbox.

Also I want to save the existing alert method so that we can switch back once our application is over.

Something like this, but its throwing error in firefox console.

window.prototype.alert = function(){

}
Milad Naseri
  • 4,053
  • 1
  • 27
  • 39
indianwebdevil
  • 4,879
  • 7
  • 37
  • 51

2 Answers2

5

There is no window.prototype object. window is a global object of javascript context and it is not created from the prototype.

However, what you want to do is achievable with the following code:

window.old_alert = window.alert;  
window.alert = function(txt) {
      // do what you need
      this.old_alert(txt);
}
Krizz
  • 11,362
  • 1
  • 30
  • 43
3

You can;

var base = window.alert;
window.alert = function(message) {
    document.getElementById("myalertwidget").innerHTML = message;
    return base.apply(this, arguments);
};
Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • `base.apply(this, arguments)` just correctly calls the *original* `alert()` should you wish to do so – Alex K. Jan 18 '12 at 11:58