dom icon indicating copy to clipboard operation
dom copied to clipboard

Increase the ability to query whether the host environment supports a specific event

Open answershuto opened this issue 4 years ago • 8 comments

We hope to solve the problem of obtaining whether the current host environment itself provides a certain event capability according to the "feature judgment" under the Web standard, so as to know whether a specific event capability can be used in the current host environment.

for example:

Take the dblclick as an example. On the mobile terminal, browsers such as safari and firefox support it, but chrome and android WebView do not. At this time, if we don't have "feature judgment", we need to code like the following.

if (/Chrome/.test(window.navigator.userAgent)) {
  // Realize by other means, such as judging 2 clicks
} else {
  element.addEventListener('dblclick', (e)=>{
    //...
  });
}

Obviously, we can implement related functions through "environmental judgment", but when the historical version and the variety of browsers bring a lot of judgment conditions.

We expect to expand on eventTarget to solve this problem.

More detailed content - https://github.com/answershuto/isEventSupports.

answershuto avatar Apr 13 '21 07:04 answershuto

The way this is down is through event handler attributes, e.g., "ondblclick" in element. Why is that not sufficient?

annevk avatar Apr 13 '21 09:04 annevk

The reason is explained in this document.

https://github.com/answershuto/isEventSupports#other-considerations

answershuto avatar Apr 13 '21 09:04 answershuto

@answershuto

At this time, input will return true normally, but in fact there will be no input event on the div.

This is quite random ... as soon as the div has contentEditable set to true, or as attribute, it supports input events.

Any other concrete example that wouldn't fail expectations? 'cause divs do support input events.

WebReflection avatar Apr 13 '21 09:04 WebReflection

@answershuto I read that, but DOM0 and DOM2 no longer exist so I don't really see how that applies in today's world.

annevk avatar Apr 13 '21 09:04 annevk

@WebReflection What about onload ?

answershuto avatar Apr 13 '21 09:04 answershuto

If 'onxxxx' in window is used for detection, can 'onxxxx' ensure one-to-one correspondence with supported events?

answershuto avatar Apr 13 '21 09:04 answershuto

Generally we try to maintain that, yes. Of course, sometimes with elements onx could be true, but events of that name don't get dispatched on that element (yet), but I don't think we've run into a problem in practice with that.

annevk avatar Apr 13 '21 10:04 annevk

@answershuto what about it?

const div = document.createElement('div');
div.addEventListener('load', console.log, true);
document.body.appendChild(div).innerHTML = '<img src="/favicon.ico">';

Works pretty well to me.

WebReflection avatar Apr 13 '21 10:04 WebReflection