Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Custom Modernizr tests that are useful.
/* modernizr-test.js
* Daniel Ott
* 3 March 2011
* Custom Tests using Modernizr's addTest API
/* iOS
* There may be times when we need a quick way to reference whether iOS is in play or not.
* While a primative means, will be helpful for that.
Modernizr.addTest('ipad', function () {
return !!navigator.userAgent.match(/iPad/i);
Modernizr.addTest('iphone', function () {
return !!navigator.userAgent.match(/iPhone/i);
Modernizr.addTest('ipod', function () {
return !!navigator.userAgent.match(/iPod/i);
Modernizr.addTest('appleios', function () {
return (Modernizr.ipad || Modernizr.ipod || Modernizr.iphone);
/* CSS position:fixed
* Not supported in older IE browsers, nor on Apple's iOS devices.
* Actually the token example on the Modernizr docs.
Modernizr.addTest('positionfixed', function () {
var test = document.createElement('div'),
control = test.cloneNode(false),
fake = false,
root = document.body || (function () {
fake = true;
return document.documentElement.appendChild(document.createElement('body'));
var oldCssText =; = 'padding:0;margin:0'; = 'position:fixed;top:42px';
var ret = test.offsetTop !== control.offsetTop;
root.removeChild(control); = oldCssText;
if (fake) {
/* Uh-oh. iOS would return a false positive here.
* If it's about to return true, we'll explicitly test for known iOS User Agent strings.
* "UA Sniffing is bad practice" you say. Agreeable, but sadly this feature has made it to
* Modernizr's list of undectables, so we're reduced to having to use this. */
return ret && !Modernizr.appleios;
Copy link

arxpoetica commented Dec 29, 2012


Modernizr.appleios isn't sufficient, since ios5+ incorporates (true) position fixed.

See the famous threads: Modernizr/Modernizr#167 and Modernizr/Modernizr#539

Copy link

ddellostritto commented Sep 25, 2013

But as a gist this is a nice reference doc that could be increased.

For determining mobile see:

test:'all and (max-width: 1070px)'),

Copy link

pierrecholhot commented Oct 2, 2015

These tests are outdated in 2015. Simply relying on the UserAgent String isn't enough since Microsoft injected the word iPhone in IE user agent. (look for the word iPhone)

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