§ Javascript function to add css
This javascript is a way to add an extra style node (css) into a page.
This creates a style node and appends it to the head element. I use this in Greasemonkey to style other stuff I add to certain pages. I won't go into that here.
Note the var mystyle=""+<r><![CDATA[ etc., works in FF but I think it fails in IE; never tried in Safari. I don't know the ins and outs of this, but it's basically a way to cheat and have multi-line strings in javascript. I think. For other browsers you may have to make the css all in one line. If you use php, you could use the "multiline_to_single_line_javascript" function from Use iframe contents to change iframe to ajax div.
You may not have to use the setAttribute() function. Comparing the Use iframe contents to change iframe to ajax div entry, you may be able to use style.type="text/css";
function addmystyle()
{
//put the style in first so my div doesn't jump around
var styles = document.createElement('style');
styles.setAttribute('type', 'text/css');
var mystyle=""+<r><![CDATA[
.mylinksdiv a
{
float:left;
margin:1px;
padding:2px 5px;
background-color:#cccccc;
color:blue !important;
font-weight:400 !important;
opacity:.6;
}
.mylinksdiv a:hover
{
font-weight:900 !important;
}
div.mylinksdiv
{
float:left;
}
]]></r>;
var newStyle = document.createTextNode(mystyle);
styles.appendChild(newStyle);
var headRef = document.getElementsByTagName('head')[0];
headRef.appendChild(styles);
}
last edited on January 14th, 2010 at 2:20 PM
- Slashback on "window.onload via double delegate function" - The makeDoubleDelegate() function is rather simple and straightforward, so I find it quite odd that Chrome can't handle it. I do…
- Joaquin Serra on "window.onload via double delegate function" - Hello, i'm using this function one year but i realized now that in Google chrome doesn't work. Have you got some solution?
- tadd on "Javascript function to add css" - thanks for the post
- Slashback on "window.onload via double delegate function" - The makeDoubleDelegate function just needs to be available, so include it once somewhere in your html file or an included javascript…
- Neil Haskins on "window.onload via double delegate function" - I don't understand exactly how this is to be used; I've just used javascript in a cut and paste manner really. Does the whole code go…
tadd says:
thanks for the post
March 17th, 2010 at 6:00 AM