I have a CSS file, which contain the style information of my web. Some height & width is based on the screen size, so I can calculate the size using the Javascript, can I embedded the javascript in the CSS file to do so?
+3
A:
I don't think so, but you can do the opposite, i.e. make the javascript create an inline css on the fly.
klez
2009-10-25 10:41:25
+6
A:
In IE, and only IE, you can use CSS expressions:
width: expression(blah + "px");
Then width
becomes whatever's inside the brackets.
This only works in IE, though - so don't use it. Use a JS function to assign the elements the style with element.style.width
or similar.
For example:
<script type="text/javascript">
document.getElementById('header').style.width = (100 * 2) + 'px';
</script>
Lucas Jones
2009-10-25 10:43:20
Shouldn't the example use `.STYLE.width` as well?
ndim
2009-10-25 10:55:34
Now that you mention it, I'm not actually sure... I'll just check.
Lucas Jones
2009-10-25 11:05:20
Yep, you're right! :)
Lucas Jones
2009-10-25 11:07:44
remember `+'px'`
bobince
2009-10-25 11:19:37
Using expressions are so bad that they took it out of IE8
epascarello
2009-10-25 12:29:47
@bobince: Thanks. @epascarello: They did? Thank God. :)
Lucas Jones
2009-10-25 15:36:11
Or with jQuery: `$("#header").css("width", 100 * 2)`
cdmckay
2009-10-25 15:54:00
@cdmckay: Thanks for the obligatory JQuery mention! :D
Lucas Jones
2009-10-25 16:38:33