Skip to content

Commit

Permalink
initial import of matchMedia polyfill for our testMediaQuery function…
Browse files Browse the repository at this point in the history
…. this has better compat.
  • Loading branch information
paulirish committed Mar 10, 2011
1 parent 7b27f96 commit 12d5767
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions modernizr.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,28 +97,48 @@ window.Modernizr = (function(window,document,undefined){

featurename, // used in testing loop

refNode = document.getElementsByTagName('script')[0],



// matchMedia polyfill by Scott Jehl and Paul Irish
// gist.github.com/786768

// todo: consider using http://javascript.nwbox.com/CSSSupport/css-support.js instead
testMediaQuery = function(mq){

var st = document.createElement('style'),
div = document.createElement('div'),
ret;

st.textContent = mq + '{#modernizr{height:3px}}';
docHead.appendChild(st);
div.id = 'modernizr';
docElement.appendChild(div);
var cache = {},
fakeBody = document.createElement('body'),
testDiv = document.createElement('div');

testDiv.id = mod + '-mqtest';
fakeBody.appendChild(testDiv);

return function(mq){
if (cache[mq] == undefined) {
var styleBlock = document.createElement('style'),
cssrule = '@media ' + mq + ' { #' + mod + '-mqtest { position: absolute; } }';
// must set type for IE
styleBlock.type = "text/css";
if (styleBlock.styleSheet){
styleBlock.styleSheet.cssText = cssrule;
}
else {
styleBlock.appendChild(document.createTextNode(cssrule));
}
refNode.parentNode.insertBefore(fakeBody, refNode);
refNode.parentNode.insertBefore(styleBlock, refNode);
cache[mq] = ((window.getComputedStyle ? getComputedStyle(testDiv, null) : testDiv.currentStyle)['position'] == 'absolute');
fakeBody.parentNode.removeChild(fakeBody);
styleBlock.parentNode.removeChild(styleBlock);
}
return cache[mq];
};

ret = div.offsetHeight === 3;
},

st.parentNode.removeChild(st);
div.parentNode.removeChild(div);

return !!ret;

},


/**
Expand Down

0 comments on commit 12d5767

Please sign in to comment.