forked from kangax/iseventsupported
-
Notifications
You must be signed in to change notification settings - Fork 0
/
isEventSupported.js
102 lines (86 loc) · 3.29 KB
/
isEventSupported.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* @method isEventSupported
* @param {String} eventName
* @param {HTMLElement} element optional
* @return {Boolean} true if event is supported
*
* Note that `isEventSupported` can give false positives when passed augmented host objects, e.g.:
*
* someElement.onfoo = function(){ };
* isEventSupported('foo', someElement); // true (even if "foo" is not supported)
*
* Also note that in Gecko clients (those that utilize `setAttribute` -based detection) -
*
* `isEventSupported('foo', someElement)`;
*
* - might create `someElement.foo` property (if "foo" event is supported) which apparently can not be deleted
* `isEventSupported` sets such property to `undefined` value, but can not fully remove it
*
*/
var isEventSupported = (function(undef) {
var TAGNAMES = {
'select':'input','change':'input',
'submit':'form','reset':'form',
'error':'img','load':'img','abort':'img'
};
function isEventSupported(eventName, element) {
element = element || document.createElement(TAGNAMES[eventName] || 'div');
eventName = 'on' + eventName;
var isSupported = (eventName in element);
if (!isSupported) {
// if it has no `setAttribute` (i.e. doesn't implement Node interface), try generic element
if (!element.setAttribute) {
element = document.createElement('div');
}
if (element.setAttribute && element.removeAttribute) {
element.setAttribute(eventName, '');
isSupported = typeof element[eventName] == 'function';
// if property was created, "remove it" (by setting value to `undefined`)
if (typeof element[eventName] != 'undefined') {
element[eventName] = undef;
}
element.removeAttribute(eventName);
}
}
element = null;
return isSupported;
}
return isEventSupported;
})();
var isEventSupportedWithCache = (function(undef) {
var TAGNAMES = {
'select':'input','change':'input',
'submit':'form','reset':'form',
'error':'img','load':'img','abort':'img'
},
cache = { };
function isEventSupported(eventName, element) {
var canCache = (arguments.length == 1);
// only return cached result when no element is given
if (canCache && cache[eventName]) {
return cache[eventName];
}
element = element || document.createElement(TAGNAMES[eventName] || 'div');
eventName = 'on' + eventName;
// When using `setAttribute`, IE skips "unload", WebKit skips "unload" and "resize", whereas `in` "catches" those
var isSupported = (eventName in element);
if (!isSupported) {
// if it has no `setAttribute` (i.e. doesn't implement Node interface), try generic element
if (!element.setAttribute) {
element = document.createElement('div');
}
if (element.setAttribute && element.removeAttribute) {
element.setAttribute(eventName, '');
isSupported = typeof element[eventName] == 'function';
// if property was created, "remove it" (by setting value to `undefined`)
if (typeof element[eventName] != 'undefined') {
element[eventName] = undef;
}
element.removeAttribute(eventName);
}
}
element = null;
return canCache ? (cache[eventName] = isSupported) : isSupported;
}
return isEventSupported;
})();