paulcpederson / focusin

Focusin/focusout polyfill for Firefox
ISC License
5 stars 1 forks source link

Don't polyfill when focusin is supported #4

Open jamiebuilds opened 8 years ago

jamiebuilds commented 8 years ago

This is not enough to detect browser support:

window.onfocusin === undefined

Safari and Chrome both support focusin but window.onfocusin will be undefined.

There's also an issue of this polyfill running more than once even after doing it's job. This is an issue if you have multiple copies for some reason or if you call polyfill() more than once.

paulcpederson commented 8 years ago

That's a good point about the multiple bindings if you can it more than once. Open to suggestions about that.

As per accurate browser support, that is also a good point. http://stackoverflow.com/questions/7337670/how-to-detect-focusin-support seems to indicate that the only way is to make a focusable element and fire focus on that element, which seems really obtrusive and gross. Not sure what to do about that one.

jamiebuilds commented 8 years ago

The only accurate way I can find to test for this is to:

var el = document.createElement('input'),
  prev = document.activeElement,
  supported = false;

function callback() {
  supported = true;
}

if (window.addEventListener) {
  el.addEventListener('focusin', callback);
} else if (window.attachEvent) {
  el.attachEvent('onfocusin', callback);
}

document.body.appendChild(el);

el.focus();

if (prev) {
  prev.focus();
} else {
  el.blur();
}

document.body.removeChild(el);

http://jsbin.com/pinoyi/edit?js,console

jdalton commented 8 years ago

In pre-Edge you could check with 'onfocusin' in window but Edge removed the inference so it's a bit trickier. Related to https://github.com/jquery/jquery/issues/2389#issuecomment-148947869.

jamiebuilds commented 8 years ago

@jdalton What does this result in for Edge? (sorry, can't test right now)

var el = document.createElement('input');
'onfocusin' in el; // Safari: true, Chrome: false
jdalton commented 8 years ago

false

jamiebuilds commented 8 years ago

damnit