modernizr
Version:
Modernizr is a JavaScript library that detects HTML5 and CSS3 features in the user’s browser.
60 lines (51 loc) • 2.38 kB
JavaScript
define(['ModernizrProto', 'createElement'], function( ModernizrProto, createElement ) {
// isEventSupported determines if the given element supports the given event
// kangax.github.com/iseventsupported/
// github.com/Modernizr/Modernizr/pull/636
//
// Known incorrects:
// Modernizr.hasEvent("webkitTransitionEnd", elem) // false negative
// Modernizr.hasEvent("textInput") // in Webkit. github.com/Modernizr/Modernizr/issues/333
var isEventSupported = (function (undefined) {
// Detect whether event support can be detected via `in`. Test on a DOM element
// using the "blur" event b/c it should always exist. bit.ly/event-detection
var needsFallback = !('onblur' in document.documentElement);
/**
* @param {string|*} eventName is the name of an event to test for (e.g. "resize")
* @param {(Object|string|*)=} element is the element|document|window|tagName to test on
* @return {boolean}
*/
function isEventSupportedInner( eventName, element ) {
var isSupported;
if ( !eventName ) { return false; }
if ( !element || typeof element === 'string' ) {
element = createElement(element || 'div');
}
// Testing via the `in` operator is sufficient for modern browsers and IE.
// When using `setAttribute`, IE skips "unload", WebKit skips "unload" and
// "resize", whereas `in` "catches" those.
eventName = 'on' + eventName;
isSupported = eventName in element;
// Fallback technique for old Firefox - bit.ly/event-detection
if ( !isSupported && needsFallback ) {
if ( !element.setAttribute ) {
// Switch to generic element if it lacks `setAttribute`.
// It could be the `document`, `window`, or something else.
element = createElement('div');
}
if ( element.setAttribute && element.removeAttribute ) {
element.setAttribute(eventName, '');
isSupported = typeof element[eventName] === 'function';
if ( element[eventName] !== undefined ) {
// If property was created, "remove it" by setting value to `undefined`.
element[eventName] = undefined;
}
element.removeAttribute(eventName);
}
}
return isSupported;
}
return isEventSupportedInner;
})();
return isEventSupported;
});