Created
July 27, 2010 20:48
-
-
Save paulirish/492840 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// modernizr plugin for touch detection | |
// mostly by jdalton. | |
// The catch is that Modernizr.touch cannot be checked before the first mousedown/click/touch of the user | |
// lazily add listeners | |
// http://dl.dropbox.com/u/513327/touch_event/test2.html | |
// http://www.quirksmode.org/blog/archives/2010/02/do_we_need_touc.html#link4 | |
(function() { | |
var hasTouchEvents = false; | |
if (!document.addEventListener){ | |
report(); // fail the test if IE event model | |
return; | |
} | |
function touchstart() { | |
hasTouchEvents = true; | |
this.removeEventListener('mousedown', mousedown, true); | |
this.removeEventListener('touchstart', touchstart, true); | |
// hookup bubble phase touch event handlers | |
// foo.addEventListener('touchstart', handler, false); | |
report(); | |
} | |
// use an event that is triggered just after touchstart | |
function mousedown() { | |
this.removeEventListener('mousedown', mousedown, true); | |
// hookup regular bubble phase event handlers here | |
// foo.addEventListener('click', handler, false); | |
report(); | |
} | |
function report(){ | |
Modernizr.addTest('touch',function(){ return hasTouchEvents; }); | |
} | |
document.addEventListener('mousedown', mousedown, true); | |
document.addEventListener('touchstart', touchstart, true); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this is deprecated cuz chrome 5 is dead now.
back to an easy 'ontouchstart' in document or whatever check