简体   繁体   中英

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM