Question

I need to know if a user is using a mobile touch device in my JavaScript code to not register any hover eventHandlers. Since I don't want to use userAgent sniffing like:

isMobile = function() {
  return /iPhone|iPod|iPad|Android|BlackBerry/.test(navigator.userAgent);
};

I'd expected conditionizr gives me something like conditionizr.isTouch. The solution I came up with is to test for the touch class like this:

isMobile = function() {
  return $('#conditionizr').hasClass('touch');
};

This works because conditionizr adds the class touch to the html

<html id="conditionizr" class=" chrome no-retina no-touch mac">

But I guess there is a better way to access the settings right?

Was it helpful?

Solution

Update on Conditionizr v4 release

The new Conditionizr APIs do exactly what you want above:

if (conditionizr.touch) {
    // touch
}

... As well as many other brilliant enhancements such as the .on() API:

conditionizr.on('chrome', function () {
    // callback for Chrome
});

And much more!

http://conditionizr.com

OTHER TIPS

The settings and internal variables are not exposed, but you can simply use the same check conditionizr is using:

isMobile = function() {
  return ('ontouchstart' in window)
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top