Permalink
Browse files

refactored the eminpx function a bit, but it'll need to be called way…

…y less frequently in the end.
  • Loading branch information...
1 parent dd4dd4f commit 840e8b81187ca652622723d7ba5f8bd12bf3683c scottjehl committed Jan 16, 2012
Showing with 23 additions and 19 deletions.
  1. +23 −19 respond.src.js
View
@@ -46,10 +46,8 @@ window.matchMedia = window.matchMedia || (function(doc, undefined){
//define vars
var doc = win.document,
docElem = doc.documentElement,
- refNode = docElem.firstElementChild || docElem.firstChild,
- // fakeBody required for <FF4 when executed in <head>
- fakeUsed = !doc.body,
- fakeBody = doc.body || doc.createElement( "body" ),
+ refNode = docElem.firstChild,
+ fakeBody = doc.body || doc.createElement( "body" ),
mediastyles = [],
rules = [],
appendedEls = [],
@@ -176,26 +174,32 @@ window.matchMedia = window.matchMedia || (function(doc, undefined){
lastLink = links[ links.length-1 ],
now = (new Date()).getTime(),
eminpx = (function() {
- var ret,
- div = doc.createElement('div');
+ var ret,
+ div = doc.createElement('div'),
+ body = doc.body,
+ fakeUsed = false;
- div.style.cssText = "position:absolute;top:-99em;width:1em";
- fakeBody.appendChild( div );
+ div.style.cssText = "position:absolute;font-size:1em;width:1em";
+
+ if( !body ){
+ body = fakeUsed = fakeBody;
+ }
+
+ body.appendChild( div );
- if( fakeUsed ){
- docElem.insertBefore( fakeBody, refNode );
- }
+ docElem.insertBefore( body, docElem.firstChild );
- ret = div.offsetWidth;
+ ret = div.offsetWidth;
- if( fakeUsed ){
- docElem.removeChild( fakeBody );
- }
-
- fakeBody.removeChild( div );
+ if( fakeUsed ){
+ docElem.removeChild( body );
+ }
+ else {
+ body.removeChild( div );
+ }
- return parseFloat(ret);
- })();
+ return parseFloat(ret);
+ })();
//throttle resize calls
if( fromResize && lastCall && now - lastCall < resizeThrottle ){

0 comments on commit 840e8b8

Please sign in to comment.