Skip to content

Instantly share code, notes, and snippets.

@paulirish
Created July 27, 2010 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulirish/492840 to your computer and use it in GitHub Desktop.
Save paulirish/492840 to your computer and use it in GitHub Desktop.
// 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);
})();
@paulirish
Copy link
Author

this is deprecated cuz chrome 5 is dead now.

back to an easy 'ontouchstart' in document or whatever check

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment