Created
April 24, 2012 15:50
-
-
Save m90/2480920 to your computer and use it in GitHub Desktop.
modernizr media query testing and hair pulling
This file contains hidden or 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
window.matchMedia = window.matchMedia || (function(doc, undefined){ | |
var bool, | |
docElem = doc.documentElement, | |
refNode = docElem.firstElementChild || docElem.firstChild, | |
// fakeBody required for <FF4 when executed in <head> | |
fakeBody = doc.createElement('body'), | |
div = doc.createElement('div'); | |
div.id = 'mq-test-1'; | |
div.style.cssText = "position:absolute;top:-100em"; | |
fakeBody.style.background = "none"; | |
fakeBody.appendChild(div); | |
return function(q){ | |
div.innerHTML = '­<style media="'+q+'"> #mq-test-1 { width: 42px; }</style>'; | |
docElem.insertBefore(fakeBody, refNode); | |
bool = div.offsetWidth == 42; | |
docElem.removeChild(fakeBody); | |
return { matches: bool, media: q }; | |
}; | |
})(document); |
This file contains hidden or 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
testMediaQuery = function( mq ) { | |
var matchMedia = window.matchMedia || window.msMatchMedia; | |
if ( matchMedia ) { | |
return matchMedia(mq).matches; | |
} | |
var bool; | |
injectElementWithStyles('@media ' + mq + ' { #' + mod + ' { position: absolute; } }', function( node ) { | |
bool = (window.getComputedStyle ? | |
getComputedStyle(node, null) : | |
node.currentStyle)['position'] == 'absolute'; | |
}); | |
return bool; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment