最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Call function when alert message is fired - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited Dec 23, 2014 at 4:15 Ian Hazzard 7,7708 gold badges37 silver badges61 bronze badges asked Dec 23, 2014 at 1:03 CoderCoder 1691 gold badge8 silver badges17 bronze badges 10
  • 1 The alert function itself does not trigger any events. – Anthony Forloney Commented Dec 23, 2014 at 1:08
  • so what should i do when alert message is fired then i need to call another method @AnthonyForloney – Coder Commented Dec 23, 2014 at 1:09
  • 5 My suggestion would be to use something more intuitive and user-friendly than the native alert functionality, such as jQuerys dialog function and hook into that. – Anthony Forloney Commented Dec 23, 2014 at 1:10
  • @Anthony Forloney, I couldn't agree more! – Marventus Commented Dec 23, 2014 at 1:11
  • 3 not to mention that alert() blocks other script running – charlietfl Commented Dec 23, 2014 at 2:00
 |  Show 5 more ments

2 Answers 2

Reset to default 2

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.

发布评论

评论列表(0)

  1. 暂无评论