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

jquery - In JavaScript, what is event.isTrigger? - Stack Overflow

programmeradmin19浏览0评论

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
  • 7 That's pretty ugly. if (!e.isTrigger) is how that should be written. If jQuery ever starts setting it to false explicitly, this code will break in a pretty messy way. – user229044 Commented Jan 9, 2013 at 23:01
  • 9 As e.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
  • Indeed is not available anymore in jQuery.. – Kamafeather Commented Jul 26, 2023 at 1:14
Add a comment  | 

3 Answers 3

Reset to default 40

In 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.');
};
发布评论

评论列表(0)

  1. 暂无评论