PDA

View Full Version : CSS bug?


TwoD
07-14-04, 05:36 AM
I noticed that if you use javascript to print a stylesheet tag onto your page in any other way than with document.write() it won't be added to the page. Atleast not in IE6 which is what I've tested it in.

Here's an example:


<html>
<body>
<script>
document.write("<style>.Test{color:blue}</style>") // Will work
//document.body.innerHTML += "<style>.Test{color:blue}</style>" //Won't work
//document.body.innerHTML += "a<style>.Test{color:blue}</style>" //Will work
//document.body.insertAdjacentHTML("beforeEnd","<style>.Test{color:blue}</style>") // Won't work
//document.body.insertAdjacentHTML("beforeEnd","a<style>.Test{color:blue}</style>") //Will work
</script>
<div class="Test">Test</div>
</body>
</html>


Just remove/add the comments to see what I mean.
It doesn't have to be an a before the tag, any character(s) will do.

Does anyone know why this happens?

Ethan
07-14-04, 06:46 AM
Style tags have to be in the head tag, not the body tag.

TwoD
07-14-04, 07:28 AM
No they don't. Now you can place style tags anywhere, including inside div tags if you wish, not all browser version supports it though but when deciding which browser to use for Javascript programming everything but the latest IE can be excluded....
And placing the dynamically written styles I gave as an examle in the head tag won't fix the annoying problem.