Controlling css with javascript works with Mozilla & Chrome but not IE?

"YOU AND THE ART OF ONLINE DATING" is the only product on the market that will take you step-by-step through the process of online dating, provide you with the resources to help ensure success. Get it now!

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.

Var style = document. CreateElement('style'); 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.Style.

Rel = 'stylesheet'; style. Href = 'FireFox. Css'; There's no such properties on an HTMLStyleElement.

Contains inline code. For external stylesheets, use a . By luck, it happens this does work: var link= document.

CreateElement('link'); link. Rel= 'stylesheet'; link. Href= 'something.

Css'; head. AppendChild(link); 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 ) by using the document.

StyleSheets interface. Unfortunately, IE's interface doesn't quite match the standard here so you need code branching: var style= document. StyleSheets0; if ('insertRule' in style) style.

InsertRule('p { margin: 0; }', 0); else if ('addRule' in style) style. AddRule('p', 'margin: 0', 0).

This has been tested to work on all major browsers (Chrome/Safari/FF/Opera/IE) including IE6,7+8: function createCSS(css, doc) { doc = doc || document; var style = doc. CreateElement("style"); style. Type = "text/css"; if (!window.

Opera && 'styleSheet' in style && 'cssText' in style. StyleSheet) { // Internet Explorer 6-8 don't support adding text nodes to // styles, so use the proprietary `styleSheet. CssText` instead style.styleSheet.

CssText = css; } else { // Otherwise use the standard method style. AppendChild(doc. CreateTextNode(css)); } // Note the `|| document.

Body` as getting the // head doesn't always work on e.g. Safari 1.0 var head = doc. GetElementsByTagName("head")0 || doc. Body; // Add the new style of higher priority than the last // ones if there's already other elements in the head if (head.

FirstChild) { head. InsertBefore(style, head. FirstChild); } else { head.

AppendChild(style); } } 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 standard createTextNode when possible and cleaned various things up.

This looks like a very legitimate answer;not that the other answers aren't that bad. Going to do some testing 1st, before I pick it as the accepted answer. THANK YOU very much, if it does work.

– GlassGhost Apr 26 '10 at 17:23 @GlassGhost: no worries - I use it in my web app so it should be fairly heavily tested – David Morrissey Apr 26 '10 at 22:34 No need for the delete _CSS. It won't cause an error but it doesn't do anything since only properties of objects may be deleted. – Box9 Jan 6 at 10:21 @Box9: Yeah, you're right.

I rewrote the style of that a bit too, I'm not much of a fan of CamelCase any more – David Morrissey Jan 6 at 20:35 1 @xavierm02: IE6 gives a type mismatch error if there isn't whitespace before the tag. The last whitespace was not needed it seems though, so I removed it. – David Morrissey May 1 at 2:44.

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!): function addHtmlStyles( sCss, oBefore ) { var oHead = document.

GetElementsByTagName('head')0; if(!oHead || oHead == null ) { return false; } var bResult = false, oStyle = document. CreateElement('style'); oStyle. Type = 'text/css'; oStyle.

Rel = 'stylesheet'; oStyle. Media = 'screen'; if( typeof oStyle. StyleSheet == 'object' ) { // IE route (and opera) try{ oStyle.styleSheet.

CssText = sCss; bResult = true; } catch(e) {} } else { // Mozilla route try{ oStyle. InnerHTML = sCss; bResult = true; } catch(e) {}; if(!bResult ) { // Webkit route try{ oStyle. InnerText = sCss; bResult = true; } catch(e) {}; } } if( bResult ) { try { if( typeof oBefore == 'object' ) { oHead.

InsertBefore(oStyle, oBefore ); } else { oHead. AppendChild(oStyle); } } catch(e) { bResult = false; } } return bResult; } It returns true when ok or false when fail. For example: var sCss = '#tooltip {background:"#FF0000";}'; // get first stylesheet with jQuery, we don't use $('head') because it not always working // If there is no stylesheet available, it will be added at the end of the head tag.

Var oHead = $(document. GetElementsByTagName('head')0), oFirst = oHead. Find('rel=stylesheet').

Get(0); if( addHtmlStyles( sCss, oFirst )) { alert( 'OK' ); } else { alert( 'NOT OK' ); } That's all. Hope you like the solution. Greetz, Erwin Haantjes.

I could not get it working with the answer as viewed, however I got it half way working by merging my original code and yours into a new function, definitely working in firefox and mostly working in chrome. – GlassGhost Dec 19 '10 at 8:15 Weird it's not working in chrome at all now maybe it updated itself or something. – GlassGhost Jan 10 at 19:02 why doesn't mine work well?

Please elaborate. Unlike this answer, mine works using non-browser specific CSS text attributes, which makes it more likely to work in the future as browsers change. Plus without Safari 1.0 head fixes/hacks yours won't work on that browser either (not that it really matters anymore as so few people use it) – David Morrissey May 1 at 8:14 Well, when I tested it; it did not work with Chrome, or Internet explorer.

Also I did add your head fix to the code provided which now works with everything but IE8 & earlier and is more terse than your code. Sorry I forgot to upvote it. Also it's been a while since I tested your code it may be working now with chrome as they are contiuously updating.At the current rate half the bounty looks like it will go to the person with 3 upvotes, as I cannot get any code recieved on this page to work with IE8 and earlier.

I'm leaving this unanswered, unless IE7(& hopefully IE6) can be made to work. – GlassGhost May 5 at 0:02 @GlassGhost: sorry, I didn't see your code Wikipedia styles. I've uploaded a modified version of your code example with my code to work in IE6-8 to: en.wikipedia.

Org/wiki/User:Frozball/monobook.js. Another issue was trying to add the CSS styles before the document was loaded - you often need to wait for documents to load before manipulating stuff, so I changed it a bit :P – David Morrissey May 5 at 7:39.

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: $(document). Ready( function() { ....... }); Also sometimes it helps to change the order of loading scripts.

Greetz, Erwin Haantjes.

Also in my case; a wikipedia style, I can't seem to access when the script is loaded or the order of loading of scripts. – GlassGhost Jan 11 at 17:36 I test the function 'addHtmlStyles' in several browsers. I don't understand your question.

A wikipedia style? Access? Greetz Erwin Haantjes – Erwinus Jan 22 at 19:30.

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): function add_css_style(css_rules, document) { document = document || self. Document; var style = document. CreateElementNS("w3.org/1999/xhtml", "style"); style.

Type = "text/css"; style. AppendChild(document. CreateTextNode(css_rules)); document.documentElement.

AppendChild(style); } If you want to use the CSSOM instead: function add_css_style(css_rules, document) { document = document || self. Document; var stylesheet = document.documentElement. AppendChild( this.

CreateElementNS("w3.org/1999/xhtml", "style") ). Sheet; stylesheet. InsertRule("@media all{" + rules + "}", 0); }.

Not workin for me with IE8(IE7, IE6 I didn't check but I don't expect anything) – GlassGhost May 2 at 2:18 I said current. IE9 is the current IE. – Eli Grey May 2 at 22:56.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions