dom
dom copied to clipboard
Increase the ability to query whether the host environment supports a specific event
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.
The way this is down is through event handler attributes, e.g., "ondblclick" in element. Why is that not sufficient?
The reason is explained in this document.
https://github.com/answershuto/isEventSupports#other-considerations
@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.
@answershuto I read that, but DOM0 and DOM2 no longer exist so I don't really see how that applies in today's world.
@WebReflection What about onload ?
If 'onxxxx' in window is used for detection, can 'onxxxx' ensure one-to-one correspondence with supported events?
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.
@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.