From 8016973743906889fa021a4d22624d0f1cf5953e Mon Sep 17 00:00:00 2001 From: scottjehl Date: Mon, 16 Jan 2012 13:15:08 +0700 Subject: [PATCH] By appending the style element before inserting CSS rules, we can prevent a crash in IE when the HTML element has a background image applied. Also, this fix allows attribute selectors to continue working in IE7 after Respond runs through. It may fix other IE instability issues, but so far, just these two: Fixes #83, Fixes #69 --- respond.min.js | 2 +- respond.src.js | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/respond.min.js b/respond.min.js index b7fe2f47..4cc5a015 100644 --- a/respond.min.js +++ b/respond.min.js @@ -1,2 +1,2 @@ /*! Respond.js v1.0.1pre: min/max-width media query polyfill. (c) Scott Jehl. MIT/GPLv2 Lic. j.mp/respondjs */ -(function(e,h){e.respond={};respond.update=function(){};respond.mediaQueriesSupported=h;if(h){return}var u=e.document,r=u.documentElement,i=[],k=[],p=[],o={},g=30,f=u.getElementsByTagName("head")[0]||r,b=f.getElementsByTagName("link"),d=[],a=function(){var B=b,w=B.length,z=0,y,x,A,v;for(;z=F.minw)&&(!F.maxw||F.maxw&&D<=F.maxw)){if(!z[F.media]){z[F.media]=[]}z[F.media].push(k[F.rules])}}for(var y in p){if(p[y]&&p[y].parentNode===f){f.removeChild(p[y])}}for(var y in z){var G=u.createElement("style"),A=z[y].join("\n");G.type="text/css";G.media=y;if(G.styleSheet){G.styleSheet.cssText=A}else{G.appendChild(u.createTextNode(A))}C.appendChild(G);p.push(G)}f.insertBefore(C,B.nextSibling)},n=function(v,x){var w=c();if(!w){return}w.open("GET",v,true);w.onreadystatechange=function(){if(w.readyState!=4||w.status!=200&&w.status!=304){return}x(w.responseText)};if(w.readyState==4){return}w.send(null)},c=(function(){var v=false;try{v=new XMLHttpRequest()}catch(w){v=new ActiveXObject("Microsoft.XMLHTTP")}return function(){return v}})();a();respond.update=a;function s(){j(true)}if(e.addEventListener){e.addEventListener("resize",s,false)}else{if(e.attachEvent){e.attachEvent("onresize",s)}}})(this,(function(f){if(f.matchMedia){return true}var e,i=document,c=i.documentElement,g=c.firstElementChild||c.firstChild,h=!i.body,d=i.body||i.createElement("body"),b=i.createElement("div"),a="only all";b.id="mq-test-1";b.style.cssText="position:absolute;top:-99em";d.appendChild(b);b.innerHTML='_';if(h){c.insertBefore(d,g)}b.removeChild(b.firstChild);e=b.offsetWidth==9;if(h){c.removeChild(d)}else{d.removeChild(b)}return e})(this)); \ No newline at end of file +(function(e,h){e.respond={};respond.update=function(){};respond.mediaQueriesSupported=h;if(h){return}var u=e.document,r=u.documentElement,i=[],k=[],p=[],o={},g=30,f=u.getElementsByTagName("head")[0]||r,b=f.getElementsByTagName("link"),d=[],a=function(){var B=b,w=B.length,z=0,y,x,A,v;for(;z=E.minw)&&(!E.maxw||E.maxw&&C<=E.maxw)){if(!y[E.media]){y[E.media]=[]}y[E.media].push(k[E.rules])}}for(var z in p){if(p[z]&&p[z].parentNode===f){f.removeChild(p[z])}}for(var z in y){var F=u.createElement("style"),A=y[z].join("\n");F.type="text/css";F.media=z;f.insertBefore(F,B.nextSibling);if(F.styleSheet){F.styleSheet.cssText=A}else{F.appendChild(u.createTextNode(A))}p.push(F)}},n=function(v,x){var w=c();if(!w){return}w.open("GET",v,true);w.onreadystatechange=function(){if(w.readyState!=4||w.status!=200&&w.status!=304){return}x(w.responseText)};if(w.readyState==4){return}w.send(null)},c=(function(){var v=false;try{v=new XMLHttpRequest()}catch(w){v=new ActiveXObject("Microsoft.XMLHTTP")}return function(){return v}})();a();respond.update=a;function s(){j(true)}if(e.addEventListener){e.addEventListener("resize",s,false)}else{if(e.attachEvent){e.attachEvent("onresize",s)}}})(this,(function(f){if(f.matchMedia){return true}var e,i=document,c=i.documentElement,g=c.firstElementChild||c.firstChild,h=!i.body,d=i.body||i.createElement("body"),b=i.createElement("div"),a="only all";b.id="mq-test-1";b.style.cssText="position:absolute;top:-99em";d.appendChild(b);b.innerHTML='_';if(h){c.insertBefore(d,g)}b.removeChild(b.firstChild);e=b.offsetWidth==9;if(h){c.removeChild(d)}else{d.removeChild(b)}return e})(this)); \ No newline at end of file diff --git a/respond.src.js b/respond.src.js index 39fa24bf..d7b9d197 100644 --- a/respond.src.js +++ b/respond.src.js @@ -140,7 +140,6 @@ docElemProp = docElem[ name ], currWidth = doc.compatMode === "CSS1Compat" && docElemProp || doc.body[ name ] || docElemProp, styleBlocks = {}, - dFrag = doc.createDocumentFragment(), lastLink = links[ links.length-1 ], now = (new Date()).getTime(); @@ -181,18 +180,20 @@ ss.type = "text/css"; ss.media = i; + //originally, ss was appended to a documentFragment and sheets were appended in bulk. + //this caused crashes in IE in a number of circumstances, such as when the HTML element had a bg image set + head.insertBefore( ss, lastLink.nextSibling ); + if ( ss.styleSheet ){ ss.styleSheet.cssText = css; } else { ss.appendChild( doc.createTextNode( css ) ); } - dFrag.appendChild( ss ); + + //push to appendedEls to track for later removal appendedEls.push( ss ); } - - //append to DOM at once - head.insertBefore( dFrag, lastLink.nextSibling ); }, //tweaked Ajax functions from Quirksmode ajax = function( url, callback ) {