Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 1a6cd42b84ec748b146821893f557c3bf6ad5284 Juriy Zaytsev committed Apr 6, 2009
Showing with 160 additions and 0 deletions.
  1. +22 −0 LICENSE
  2. +15 −0 index.html
  3. +26 −0 isEventSupported.js
  4. +97 −0 test.js
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2009 Juriy Zaytsev
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,15 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
+ "http://www.w3.org/TR/html4/strict.dtd">
+<html lang="en">
+ <head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+ <title>isEventSupported</title>
+ <style type="text/css" media="screen">
+ span { padding: 0 0.5em; }
+ </style>
+ </head>
+ <body>
+ <script src="isEventSupported.js" type="text/javascript"></script>
+ <script src="test.js" type="text/javascript"></script>
+ </body>
+</html>
@@ -0,0 +1,26 @@
+var isEventSupported = (function(){
+ 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];
+ }
+ var el = element || document.createElement(TAGNAMES[eventName] || 'div');
+ eventName = 'on' + eventName;
+ // When using `setAttribute`, IE skips "unload", WebKit skips "unload" and "resize"
+ // `in` "catches" those
+ var isSupported = (eventName in el);
+ if (!isSupported) {
+ el.setAttribute(eventName, 'return;');
+ isSupported = typeof el[eventName] == 'function';
+ }
+ el = null;
+ return canCache ? (cache[eventName] = isSupported) : isSupported;
+ }
+ return isEventSupported;
+})();
97 test.js
@@ -0,0 +1,97 @@
+function w(name) {
+ document.write(name + ': ' + (
+ isEventSupported(name)
+ ? '<span style="background-color:green;color:white;">true</span>'
+ : '<span style="background-color:red;color:white;">false</span>'
+ ) + '<br>');
+}
+
+document.write('<pre>' + isEventSupported + '</pre><br><br>');
+
+document.write('<h2>Mouse events:</h2>');
+
+w('click');
+w('dblclick');
+w('mousedown');
+w('mouseup');
+w('mouseover');
+w('mousemove');
+w('mouseout');
+
+document.write('<h2>Key events:</h2>');
+
+w('keypress');
+w('keydown');
+w('keyup');
+
+document.write('<h2>HTML events</h2>');
+
+w('load');
+w('unload');
+w('abort');
+w('error');
+
+document.write('<h2>View events</h2>');
+
+w('resize');
+w('scroll');
+
+document.write('<h2>Form events:</h2>');
+
+w('submit');
+w('reset');
+
+document.write('<h2>Form controls\' events:</h2>');
+
+w('select');
+w('change');
+
+document.write('<h2>Activation events:</h2>');
+
+w('focus');
+w('blur');
+
+document.write('<h2>MSHTML DOM events:</h2>');
+
+w('cut');
+w('copy');
+w('paste');
+w('beforecut');
+w('beforecopy');
+w('beforepaste');
+w('afterupdate');
+w('beforeupdate');
+w('cellchange');
+w('dataavailable');
+w('datasetchanged');
+w('datasetcomplete');
+w('errorupdate');
+w('rowenter');
+w('rowexit');
+w('rowsdelete');
+w('drag');
+w('dragstart');
+w('dragenter');
+w('dragover');
+w('dragleave');
+w('dragend');
+w('drop');
+w('selectstart');
+w('mouseenter');
+w('mouseleave');
+w('activate');
+w('beforeactivate');
+w('deactivate');
+w('beforedeactivate');
+w('focusin');
+w('focusout');
+w('stop');
+
+document.write('<h2>Unexistent (most likely) events:</h2>');
+
+w('click2');
+w('foobarbaz');
+
+document.write('<h2>Unexistent in Opera <=10a</h2>');
+
+w('contextmenu');

0 comments on commit 1a6cd42

Please sign in to comment.