Im having problems with this function applying css(using a text variable) working with Internet Explorer but it works in Firefox & Chrome.
/*! addCssStyle() applies the text value $CssText$ to the the specified document
$Doc$ e.g. an IFrame; or if none specified, default to the current document,
*/function addCssStyle(CssText, Doc){
//Secure $Head$ for the current $Doc$
Doc = Doc||document; var head = Doc.getElementsByTagName('head')[0];
if(!head || head == null){
head = Doc.createElement('div'); Doc.body.appendChild(head);
} if(!head || head == null){return false;}
//createElement('style')
var PendingStyle = Doc.createElement('style');
// if (is_gecko){PendingStyle.href = 'FireFox.css';}//???needeed???
PendingStyle.type = 'text/css';
PendingStyle.rel = 'stylesheet';
// PendingStyle.media = 'screen';//???needeed???
PendingStyle.innerHTML = CssText;
head.appendChild(PendingStyle);
}/*___________________________________________________________________________*/
var NewSyleText = //The page styling
"h1, h2, h3, h4, h5 {font-family: 'Verdana','Helvetica',sans-serif; font-style: normal; font-weight:normal;}" +
"body, b {background: #fbfbfb; font-style: normal; font-family: 'Cochin','GaramondNo8','Garamond','Big Caslon','Georgia','Times',serif;font-size: 11pt;}" +
"p { margin: 0pt; text-indent:2.5em; margin-top: 0.3em; }" +
"a { text-decoration: none; color: Navy; background: none;}" +
"a:visited { color: #500050;}" +
"a:active { color: #faa700;}" +
"a:hover { text-decoration: underline;}";
addCssStyle(NewSyleText);//inserts the page styling
This has been tested to work on all major browsers (Chrome/Safari/FF/Opera/IE) including IE6,7+8:
As that code is written, it is relative to the document being served so may need to be modified to make it relative to another path, or you could use absolute image paths in the CSS.
EDIT: Removed all the
innerHTML
references in favour of using the more standardcreateTextNode
when possible and cleaned various things up.I know this is a old thread but i was looking for a solution to insert CSS styles dynamicly that works with all common/major browsers. I want to share my solution with you. The solution of david doesn't work well (sorry). I have made a tooltip javascript/jQuery class that can work with inline styles for example but can also work with CSS styled styles. (offtopic: Also the class can auto align tooltips like the default tooltips).
Maybe you wonder what the benefits are when you insert CSS like the example above. Well, you don't need an extra CSS file with the styles and you can dynamicly add styles from script and when you working with images you can dynamicly change the path to the images if you want (so you don't need to change any file). Also you can insert the styles ABOVE other stylesheets/style rules and the aplied style rules can be modified in the other stylesheets below (this is not possible when you use inline styles or when placing the inserted rules BELOW any existing stylesheet).
This function works with Opera/Firefox/IE7/IE8/IE9/Chrome/Safari (without any hack applied!):
It returns true when ok or false when fail. For example:
That's all. Hope you like the solution. Greetz, Erwin Haantjes
@GlassGost: Weird is not working for you because i test it in several browsers (also on the mobile ones). Maybe it helps to add css when the DOM is ready:
Also sometimes it helps to change the order of loading scripts.
Greetz, Erwin Haantjes
This works fine for me on all current browsers with any type of document (which I assume you must be dealing with multiple documents, or otherwise you wouldn't have a
Doc
parameter):If you want to use the CSSOM instead:
Adding new stylesheets and scripts by creating elements using DOM methods is something that has always been dicey cross-browser. This won't work in IE or WebKit.
There's no such properties on an HTMLStyleElement.
<style>
contains inline code. For external stylesheets, use a<link>
. By luck, it happens this does work:But doesn't give you a convenient way to insert rules from script.
You can also add new rules to an existing stylesheet (eg. an empty
style
in the<head>
) by using thedocument.styleSheets
interface. Unfortunately, IE's interface doesn't quite match the standard here so you need code branching: