Click to See Complete Forum and Search --> : css in javascript
luvgal
02-26-2007, 07:55 AM
hi was wondering if this is the right way to include an external css in javascript?
document.write("<link href="cssStyle.css" rel="stylesheet" type="text/css" />");
i tried putting this in the head of my html but it dint work..
Nanscombe
02-26-2007, 08:34 AM
Hi luvgal,
document.write("<link href="cssStyle.css" rel="stylesheet" type="text/css" />");
It's the double quotes causing you problems. To use " inside a document.write statement you have to add a \ in front of it, also known as escaping, as it sees each " it sees the end of the string.
So it seems something like "<link href="cssStyle.css" rel="stylesheet" type= "text/css" />"
Try instead
document.write("<link href=\"cssStyle.css\" rel=\"stylesheet\" type=\"text/css\" />");
This should work.
Notice that you don't put a \ before the quotes just inside the round brackets, only in the middle of the string.
Regards
Nigel
TechEvangelist
02-26-2007, 09:31 AM
You might want to think about using JavaScript for your CSS link. I assume that you do not want search engines to find the file or you wish to swap file in your script. It should work for those purposes, but your CSS file may not be found by any visitors that have JavaScript turned off or have security set to high in Internet Explorer.
You probably want to test this by disabling JavaScript in your browser.