How do I move this javascript out of the head and into a .js to achieve valid markup?
November 13, 2006 5:44 PM
Subscribe
really basic, I think. You can't have <link /> tag inside a <script></script> using XHTML strict. I can't quite figure out how to write a function that takes the place of this basic little OS detection script <script type="text/javascript">
var cssFile = (navigator.userAgent.indexOf("Mac") != -1) ?
"mac.css" : "everyonelse.css";
document.write("<link rel='stylesheet' type='text/css' href='../" + cssFile + "' />");
</script>
Which I would put in the head of the document. How can I move it to a function in an external .js file and get the same result with valid xhtml 1.0 strict? It's not urgent, but it's driving me nuts because I'm really stupid about scripting but I know this sort of thing isn't supposed to be hard.
posted by Grod to computers & internet (17 comments total)
document.write(....
with:
var newCSS = document.createElement('link');
newCSS.setAttribute('rel', 'stylesheet');
newCSS.setAttribute('type', 'text/css');
newCSS.setAttribute('href', "../"+cssFile);
document.getElementsByTagName('head')[0].appendChild(newCSS);
posted by null terminated at 5:52 PM on November 13, 2006