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.
-
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 jQuerysdialog
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
2 Answers
Reset to default 2You 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.