Forum Moderators: open
Alternatively, it was suggested in an earlier thread that you can use document.write() to place html comments around a <style> tag that must be excluded when javascript is enabled. Similarly, you may use document.write() to insert a <style> tag.
e.g.
document.writeln('<style>');
............
document.writeln('<\/style');
NOTE
If the code is embedded in an html document, / must be escaped when it follows <
Kaled.
Set your default styles for the script disabled, then include contextual rules for enabled browsers
(Mr Marx is very politically correct this week).
[blue]
#myDiv {width: 300px; height: 500px;}
.scriptEnabled #myDiv {display:none;} /* enhance for the enabled */
[/blue] Now, the first thing you do in your script is:
[color=brown]document.getElementsByTagName('html')[0].className = "scriptEnabled";[/color] ..which can be done immediately, because the HTML element always exists
(perhaps
document.documentElement would be more up-to-date) This way, all the fiddling is kept external to the document (and neatly organised) - that's if you are using linked scripts & stylesheets, natch.
The page will validate too. Yet it could be argued that you are introducing an invalid attribute when the page loads ('class' on HTML), but at least it's just an attribute, and furthermore, Mr. Marx say's he couldn't give a tinker's cuss about that anyway.