简体   繁体   中英

Very strange IE crash problem with stylesheet javascript object

Hello I use javascript stylesheet object, with a specfic style and place it on dom ready. When i do exactly my code IE crash.

The problem is the UL with exactly this style set after the page is load. If I place the styleSheet.cssText = css; before the page load, everyting is correct. If i remove the char f in <DIV>f everything work. I need to use my code after the page is load. Any suggestion to pass over this trouble?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  <title>test</title>
<script type="text/javascript" src="./jquery-1.4.2.min.js" ></script>
<script type="text/javascript"> 
 var styleSheet = document.createStyleSheet();
 var css= "UL{list-style-type:none;display:inline;}LI{padding:0px;}";
$(document).ready(function(){
styleSheet.cssText = css;

});
</script>
</head>
<body>
<DIV>f
<UL>
<LI><a>dfgfdg</a></LI>
<LI><a>fdgdfg</a></LI></UL>?
</DIV>?
</body>
</html>

The problem is IE8 specific. It seems to work on ie7 and ie9.

In order to reproduce the bug it is important to apply the stylesheet after the page is loaded. We used jquery.ready() for this example but the code also crashes for click and load events. This bug is very specific. It requires the precise css and html used in the example above. We have tried adding the stylesheet in different ways ( stylesheet.rules[i].lisStyleType='none' for example and adding the stylesheet in a.css file) with no success. We absolutely need to add the style dynamically where this probleme is happening.

createStyleSheet is not a cross-browser friendly solution... Try this code instead:

$(function(){
    var styles = "UL{list-style-type:none;display:inline;}LI{padding:0px;}";
    var newSS=document.createElement('link');
    newSS.rel='stylesheet';
    newSS.href='data:text/css,'+escape(styles);
    document.getElementsByTagName("head")[0].appendChild(newSS);
});

I can't tell you exactly why IE is crashing, but this code works for me and it shouldn't trigger any unexpected errors.

I tried your code in a jsfiddle and it doesn't bug with IE7.. what's the problem?

What version of IE are you using? Do you have an url of a demo that's crashing for you? I can't reproduce your problem, please see my jsfiddle ...

Btw why do you use jquery 1.4.2 and not a more recent version? 1.4.4 or 1.5.2, in the jsfiddle I choosed 1.4.4

Please Try this. Tested in IE6,7,8,9 FF, Opera, Googlechrome, Safari

<script type="text/javascript">
function createRuntimeStyle(){
//create a style tag
var rStyle=document.createElement("style");

//create the content of the style tag
var cssDef=document.createTextNode(".myclass{display:block;font-family:Verdana;font-weight:bold;}");

//assign the type
rStyle.type="text/css";


if(rStyle.styleSheet){  //check for IE
rStyle.styleSheet.cssText=cssDef.nodeValue;
}else{ //check for Other Browsers
rStyle.appendChild(cssDef);
}

//append to document head
document.getElementsByTagName("head")[0].appendChild(rStyle);
}

//call to the function
createRuntimeStyle();
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM