简体   繁体   English

发出警报消息时的呼叫功能

[英]Call function when alert message is fired

I want to know which Dom event is fired when alert message is called. 我想知道调用警报消息时会触发哪个Dom事件。 I want to call start_tracking method when alert message is called. 我想在警报消息被调用时调用start_tracking方法。 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. alert()函数似乎什么也没有触发。

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. 无法捕获任何alert()函数事件。 I don't know why you would want to do that, but I would create another function instead called warn() . 我不知道为什么要这么做,但是我会创建另一个函数而不是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. 这样,每次调用warn() ,您都可以使用户alert()并调用另一个函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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