Skip to content

Instantly share code, notes, and snippets.

@rafaelcardoso
Created August 29, 2014 19:52
Show Gist options
  • Save rafaelcardoso/cffbfbd2c055fcc9b67d to your computer and use it in GitHub Desktop.
Save rafaelcardoso/cffbfbd2c055fcc9b67d to your computer and use it in GitHub Desktop.
touchtest
mod = 'modernizr';
injectElementWithStyles = function( rule, callback, nodes, testnames ) {
var style, ret, node, docOverflow,
div = document.createElement('div'),
body = document.body,
fakeBody = body || document.createElement('body');
if ( parseInt(nodes, 10) ) {
while ( nodes-- ) {
node = document.createElement('div');
node.id = testnames ? testnames[nodes] : mod + (nodes + 1);
div.appendChild(node);
}
}
style = ['&#173;','<style id="s', mod, '">', rule, '</style>'].join('');
div.id = mod;
(body ? div : fakeBody).innerHTML += style;
fakeBody.appendChild(div);
if ( !body ) {
fakeBody.style.background = '';
fakeBody.style.overflow = 'hidden';
docOverflow = docElement.style.overflow;
docElement.style.overflow = 'hidden';
docElement.appendChild(fakeBody);
}
ret = callback(div, rule);
if ( !body ) {
fakeBody.parentNode.removeChild(fakeBody);
docElement.style.overflow = docOverflow;
} else {
div.parentNode.removeChild(div);
}
return !!ret;
}
var bool;
prefixes = ' -webkit- -moz- -o- -ms- '.split(' ');
if(('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch) {
bool = true;
} else {
var query = ['@media (',prefixes.join('touch-enabled),('),'heartz',')','{#modernizr{top:9px;position:absolute}}'].join('');
injectElementWithStyles(query, function( node ) {
bool = node.offsetTop === 9;
});
}
console.log('bool', bool);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment