oninput.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*!
  2. {
  3. "name": "onInput Event",
  4. "property": "oninput",
  5. "notes": [{
  6. "name": "MDN Docs",
  7. "href": "https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers.oninput"
  8. },{
  9. "name": "WHATWG Spec",
  10. "href": "https://html.spec.whatwg.org/multipage/input.html#common-input-element-attributes"
  11. },{
  12. "name": "Related Github Issue",
  13. "href": "https://github.com/Modernizr/Modernizr/issues/210"
  14. }],
  15. "authors": ["Patrick Kettner"],
  16. "tags": ["event"]
  17. }
  18. !*/
  19. /* DOC
  20. `oninput` tests if the browser is able to detect the input event
  21. */
  22. define(['Modernizr', 'docElement', 'createElement', 'testStyles', 'hasEvent'], function(Modernizr, docElement, createElement, testStyles, hasEvent) {
  23. Modernizr.addTest('oninput', function() {
  24. var input = createElement('input');
  25. var supportsOnInput;
  26. input.setAttribute('oninput', 'return');
  27. if (hasEvent('oninput', docElement) || typeof input.oninput === 'function') {
  28. return true;
  29. }
  30. // IE doesn't support onInput, so we wrap up the non IE APIs
  31. // (createEvent, addEventListener) in a try catch, rather than test for
  32. // their trident equivalent.
  33. try {
  34. // Older Firefox didn't map oninput attribute to oninput property
  35. var testEvent = document.createEvent('KeyboardEvent');
  36. supportsOnInput = false;
  37. var handler = function(e) {
  38. supportsOnInput = true;
  39. e.preventDefault();
  40. e.stopPropagation();
  41. };
  42. testEvent.initKeyEvent('keypress', true, true, window, false, false, false, false, 0, 'e'.charCodeAt(0));
  43. docElement.appendChild(input);
  44. input.addEventListener('input', handler, false);
  45. input.focus();
  46. input.dispatchEvent(testEvent);
  47. input.removeEventListener('input', handler, false);
  48. docElement.removeChild(input);
  49. } catch (e) {
  50. supportsOnInput = false;
  51. }
  52. return supportsOnInput;
  53. });
  54. });