UNPKG

modernizr

Version:

Modernizr is a JavaScript library that detects HTML5 and CSS3 features in the user’s browser.

78 lines (66 loc) 2.55 kB
define(['ModernizrProto', 'createElement'], function(ModernizrProto, createElement) { /** * Modernizr.hasEvent() detects support for a given event * * @memberof Modernizr * @name Modernizr.hasEvent * @optionName Modernizr.hasEvent() * @optionProp hasEvent * @access public * @function hasEvent * @param {string|*} eventName - the name of an event to test for (e.g. "resize") * @param {Element|string} [element=HTMLDivElement] - is the element|document|window|tagName to test on * @returns {boolean} * @example * `Modernizr.hasEvent` lets you determine if the browser supports a supplied event. * By default, it does this detection on a div element * * ```js * hasEvent('blur') // true; * ``` * * However, you are able to give an object as a second argument to hasEvent to * detect an event on something other than a div. * * ```js * hasEvent('devicelight', window) // true; * ``` * */ var hasEvent = (function() { // 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); function inner(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'); } 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 inner; })(); ModernizrProto.hasEvent = hasEvent; return hasEvent; });