View Full Version : javascript in css
nectar
05-22-2003, 10:10 AM
Hello, does anyone know if it is possible to write javascript in a css doc? As I want some variables in javascript to change according to the css document loaded by the page. There is a css doc for larger text, but the larger text also needs larger dropdown menu - thus the needd to adjust the variables for the dropdown menu.
Thank you
brothercake
05-22-2003, 10:34 AM
No it isn't possible, but you can do it the other way round - write the CSS in javascript, for eg:
var sty = '';
sty += '<style type="text/css">';
sty += 'div.something { width:' + someObj.offsetWidth + 'px; }';
sty += '</style>';
document.write(sty);
There's an even better way, although with lower browser support, where you can rewrite CSS rules from a normal stylesheet using the DOM; more info at http://codingforums.com/showthread.php?s=&threadid=18442
realisis
05-22-2003, 11:28 AM
just as a point of historical interest:
Netscape 4 allowed you to set styles via javascript statements in the style tag.
While probably not useful to you in the present context, it's yet another viable technique whenever you need to target only NS4.
nectar
05-22-2003, 11:58 AM
thanks a lot
brothercake
05-22-2003, 02:16 PM
Yeah well IE also has a proprietary method for passing expressions to CSS
div.something { width:expression(document.body.clientWidth); }
But don't use it - quite apart from it being proprietary and non-compliant, it doesn't work properly.
realisis
05-22-2003, 02:27 PM
hmmm, wasn't aware of that one...
I'm a sucker for proprietary arcana. Thanks brothercake!
Off to experiment...
liorean
05-22-2003, 02:53 PM
Hey! I was going to mention that! You stole my idea! :rolleyes:
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.