I came across this in some JS code I was working on:
if ( typeof( e.isTrigger ) == 'undefined' ) {
// do some stuff
}
This seems to be part of jQuery. As far as I can see it tells you if an event originated with the user or automatically.
Is this right? And given that it's not documented, is there a way of finding such things out without going behind the curtain of the jQuery API?
I came across this in some JS code I was working on:
if ( typeof( e.isTrigger ) == 'undefined' ) {
// do some stuff
}
This seems to be part of jQuery. As far as I can see it tells you if an event originated with the user or automatically.
Is this right? And given that it's not documented, is there a way of finding such things out without going behind the curtain of the jQuery API?
Share Improve this question asked May 22, 2012 at 14:32 djbdjb 6,0015 gold badges44 silver badges48 bronze badges 3 |3 Answers
Reset to default 40In jQuery 1.7.2 (unminified) line 3148 contains event.isTrigger = true;
nested within the trigger function. So yes, you are correct - this is only flagged when you use .trigger()
and is used internally to determine how to handle events.
If you look at jQuery github project, inside trigger.js file line 49 (link here) you can find how isTrigger gets calculated.
If you add a trigger in your JavaScript and debug through, You can see how the breakpoint reaches this codeline (checked in jQuery-2.1.3.js for this SO question)
Modern browsers fight against popup windows opened by automated scripts, not real users clicks. If you don't mind promptly opening and closing a window for a real user click and showing a blocked popup window warning for an automated click then you may use this way:
button.onclick = (ev) => {
// Window will be shortly shown and closed for a real user click.
// For automated clicks a blocked popup warning will be shown.
const w = window.open();
if (w) {
w.close();
console.log('Real user clicked the button.');
return;
}
console.log('Automated click detected.');
};
if (!e.isTrigger)
is how that should be written. If jQuery ever starts setting it tofalse
explicitly, this code will break in a pretty messy way. – user229044 ♦ Commented Jan 9, 2013 at 23:01e.isTrigger
is not documented, it isn't promised to be kept in future releases and shouldn't be used in your production code. – rhgb Commented Nov 25, 2015 at 5:53