admin管理员组文章数量:1357374
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.');
};
本文标签: jqueryIn JavaScriptwhat is eventisTriggerStack Overflow
版权声明:本文标题:jquery - In JavaScript, what is event.isTrigger? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1737386490a1986974.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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