Coder
Coder

Reputation: 169

Call function when alert message is fired

I want to know which Dom event is fired when alert message is called. I want to call start_tracking method when alert message is called. I am able to call methods on page resize but how can i know which method is called when the alert message is fired.

win.addEventListener("click", function(event) {
      start_tracking ();
});

Click event is working when i am clicking anywhere on the page but when i click the alert message content then it does not fire the click listener. Nothing seems to be fired on the alert() function.

Upvotes: 2

Views: 9967

Answers (2)

bluetoft
bluetoft

Reputation: 5443

You could also override the native alert function...

function testAlert(){
    alert('Test Alert Warning... You clicked a button.');
} 
window.oldAlert = window.alert;
window.alert = function alert(msg){
    console.log(arguments.callee.caller.name + ' is the name of the calling function.'); // calling function...
    oldAlert(msg);
    oldAlert(arguments.callee.caller.name + ' is the name of the calling function.');
}
<button onclick="testAlert();">Warn!</button>
This will give you the calling functions name... Hope this helps.

Upvotes: 2

Ian Hazzard
Ian Hazzard

Reputation: 7771

It is not possible to capture any alert() function events. I don't know why you would want to do that, but I would create another function instead called warn().

function warn(warning) {

  alert(warning);
  capturedAlert();

}

function capturedAlert() {

  alert('An alert was called.');

}
<button onclick="warn('This is a warning!')">Warn!</button>

This way, every time warn() is called, you can alert() the user, AND call another function.

Upvotes: 1

Related Questions