modernizr
Version:
Modernizr is a JavaScript library that detects HTML5 and CSS3 features in the user’s browser.
59 lines (54 loc) • 1.94 kB
JavaScript
/*!
{
"name": "onInput Event",
"property": "oninput",
"notes": [{
"name": "MDN article",
"href": "https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers.oninput"
},{
"name": "WHATWG spec",
"href": "https://html.spec.whatwg.org/multipage/forms.html#common-input-element-attributes"
},{
"name": "Detecting onInput support",
"href": "http://danielfriesen.name/blog/2010/02/16/html5-browser-maze-oninput-support"
}],
"authors": ["Patrick Kettner"],
"tags": ["event"]
}
!*/
/* DOC
`oninput` tests if the browser is able to detect the input event
*/
define(['Modernizr', 'docElement', 'createElement', 'testStyles', 'hasEvent'], function(Modernizr, docElement, createElement, testStyles, hasEvent) {
Modernizr.addTest('oninput', function() {
var input = createElement('input');
var supportsOnInput;
input.setAttribute('oninput', 'return');
if (hasEvent('oninput', docElement) || typeof input.oninput == 'function') {
return true;
}
// IE doesn't support onInput, so we wrap up the non IE APIs
// (createEvent, addEventListener) in a try catch, rather than test for
// their trident equivalent.
try {
// Older Firefox didn't map oninput attribute to oninput property
var testEvent = document.createEvent('KeyboardEvent');
supportsOnInput = false;
var handler = function(e) {
supportsOnInput = true;
e.preventDefault();
e.stopPropagation();
};
testEvent.initKeyEvent('keypress', true, true, window, false, false, false, false, 0, 'e'.charCodeAt(0));
docElement.appendChild(input);
input.addEventListener('input', handler, false);
input.focus();
input.dispatchEvent(testEvent);
input.removeEventListener('input', handler, false);
docElement.removeChild(input);
} catch (e) {
supportsOnInput = false;
}
return supportsOnInput;
});
});