0

I have plenty of pages on my website with javascript embeded in them. alert() which is a javascript function has been used in all my scripts atleast once. I want to use custom alert box which will render same in all the browser. I can define my own function like

function customAlert()
{
    // Code goes here.
}

But have to replace alert() withcustomAlert() in all my web pages which is a time consuming process.

Instead can't we just modify native alert function accordingly and it reflects the same as in all browsers.

I think we can't use this

function alert()
{
    // Code goes here.
}

because alert is a reserved javascript word for function name.

If there is a better way for implementing the same with different technique, then feel free to answer.

I hate to use jquery or any other frameworks library so please answer regarding pure javascript.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Utkarsh Vishnoi
  • 149
  • 2
  • 14

1 Answers1

2

The alert is a property of the global window object, thus you can override it in the following way:

window.alert = function (message){
    //do your code
};

and then use it the way you used it before:

alert('hi');
Shimon Rachlenko
  • 5,469
  • 40
  • 51