This adds an tap event to an element of your choice. The tap event is like click, but it works with both mice and touch without the 300 ms delay. This was inspired by enyojs's tap event. A side note: When using inline the event object is unavailable and return false doesn't prevent default(onclick). The idea is to replace click and touchdown events with this one for better performance on touch and less clutter. Demo: http://jsfiddle.net/samarthwiz/UmE53/6/
-
-
Save s-clerc/6165762 to your computer and use it in GitHub Desktop.
<!DOCTYPE Html> | |
<html> | |
<head> | |
<script src="addOnTap.js" type="application/javascript"></script> | |
<script> | |
window.onload = function() { | |
var n = document.getElementById("Button"); | |
addOnTap(n); | |
//This works. | |
n.ontap = function(e) {alert("button tapped")} | |
//So does this | |
n.addEventListener("tap", function() { console.log("button tapped")}) | |
} | |
</script> | |
</head> | |
<body> | |
<span id="Button" ontap="console.log('INLINE BABY')" style="cursor:pointer" >Click Me</span> | |
</body> | |
</html> |
//Non-strict code | |
//Setting this code to strict mode will also set | |
//the event callbacks to strict mode. | |
(function() { | |
var fireEvent = function (element, type, data, options){ | |
var options = options || {}, | |
event = document.createEvent('Event'); | |
event.initEvent(type, 'bubbles' in options ? options.bubbles : true, 'cancelable' in options ? options.cancelable : true ); | |
for (var z in data) event[z] = data[z]; | |
element.dispatchEvent(event); | |
}; | |
var fireOnTap = function (e) { | |
fireEvent(this, "tap", e); | |
r = true; | |
if (this.ontap) { | |
if (this.ontap(e) === false) { | |
r = false; | |
} | |
} | |
if (this.getAttribute("ontap")) { | |
eval(this.getAttribute("ontap")); | |
} | |
if (e.type === "touchend") { | |
e.preventDefault(); | |
} | |
return r; | |
}; | |
window.addOnTap = function (n) { | |
n.addEventListener("touchend", fireOnTap.bind(n)); | |
n.addEventListener("mouseup", fireOnTap.bind(n)); | |
}; | |
}()); |
Copyright (C) 2013 Samarth AGARWAL
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.