Editing CSS in @media print

N

Nick

Hello,
I am trying to edit a CSS rule with Javascript, which is for printing. Thus

@media print
{
#footer { display: none; }
}

I would like to be able to access the display attribute of #footer, but
I can't get to it. If I edit the element.style.display property then
that changes it, but for all the stylesheet, not just print.

Anybody?
Thanks

Nick
 
V

VK

Nick said:
Hello,
I am trying to edit a CSS rule with Javascript, which is for printing. Thus

@media print
{
#footer { display: none; }
}

I would like to be able to access the display attribute of #footer, but
I can't get to it. If I edit the element.style.display property then
that changes it, but for all the stylesheet, not just print.

You did not specify where is your CSS table: inline or from an external
file (in the latter case it gets even more funny :).

Presuming you have an inline style declaration:

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<style>
@media all {
body { background-color: white; }
}

@media screen {
#footer { display: block; }
}

@media print {
#footer { display: none; }
}
</style>
<script type="text/javascript">
function test() {
// To get media.print::footer.display in IE:
//alert(document.styleSheets[0].rules[2].style.display);

// To get media.print::footer.display in FF:
// alert(ss.cssRules[2].cssRules[0].style.display);

// To get media.print::footer.display in Opera:
// Have no idea: window and document seems do not expose
// any scriptable object for style (?)

// Other browsers may be even more amazing (?)
}
</script>
</head>

<body onload="test()">
<div id="footer">Footer</div>
</body>
</html>

I would suggest to reverse your logic: instead of patching the CSS
table - change style attributes of elements themselve using className()
and/or setAttribute() methods. This way you'll get much more reliable
anduniversal solution.
 
T

Thomas 'PointedEars' Lahn

Nick said:
I am trying to edit a CSS rule with Javascript, which is for printing.

Not considering that this is not generally possible, why would you want to?
The very idea of CSS rules for printing is that no other technique would be
required for adequate presentation.


PointedEars
 
N

Nick

Thomas said:
Not considering that this is not generally possible, why would you want to?
The very idea of CSS rules for printing is that no other technique would be
required for adequate presentation.

Indeed. It was all a bit of a hack (and still is to some extent), after
using JS to optionally show a footer (because it uses JS to position it
(bad I know), and it'd be better non-JS to have it hidden than displayed
in the middle of the page).

Using <noscript> tags to achieve the effect instead.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top