How to check if mouse click event is simulated or is native? (non-jQuery way)

一曲冷凌霜 提交于 2019-12-04 12:57:38

There is a way, but it's quite controversial IMHO. Try this:

HTMLElement.prototype.dispatchEvent = (function(dispev) {
    return function(event) {
        // Do your stuff
        dispev.call(this, event);
    };
})(HTMLElement.prototype.dispatchEvent);

With this, you're able to catch every call to dispatchEvent when a programmatically generated event is dispatched. Of course, this would slow down a bit the dispatching, but shouldn't be a problem unless you're trying to fire a lot of events.

In IE8 you must replace Element.prototype.fireEvent instead, while in IE7 and lower it's just not possible to do anything about it.

Event.isTrusted can be used upto a satisfactory level.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!