Hi friends, Im trying to insert css with javascript in a static page. I want a certain piece of css on my specific category page, eg category is "Marketing" I want javascript to check my category and add css. eg if category is marking then it should add the my "internal css" in page code while google around I found it. Its not the exact code but It may can help you to help me.. but the javascript condtional funtion is missing function injectCSS() { var headTag = document.getElementsByTagName("head")[0].innerHTML; var newCSS = headTag + '<style type="text/css">*insert CSS here*</style>'; document.getElementsByTagName('head')[0].innerHTML += newCSS; } Code (markup):
I wouldn't try to modify the <head>. I doubt it would work on all browsers even if you could. Modify the stylesheet with JavaScript instead.
Not necessarily. Some browsers might recognize a <style> tag in the <body> because they try to be very tolerant of HTML errors, but technically, the <style> tag should only appear in the <head> and browsers are free to ignore it. As long as you're using JavaScript to do this, do it the way I suggested. It's easier, the code I wrote gives you functions you can call that make it dirt simple, and it's much more reliable because you're doing it the right way.