• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 559
  • Last Modified:

How to swap CSS files at runtime?

I am trying to set up a web page so that when the user clicks the browser's PRINT button, the page prints just as displayed (or close to it, etc).

I am also setting up a separate rollover button called 'print this page' that will only print the text of the page, hiding all other elements.

I have created a stylesheet_print.css that adequately hides the elements I wish to hide during 'print this page' processing, but I only wish that stylesheet to be employed when the user clicks the 'print this page' link that I have set up. If the user clicks the browser's own print button, I want everything on the entire page to print.

So the question is: How can I set up my page so that the stylesheet_print.css ONLY gets used when the user clicks the rollover I set up, and NOT when the user clicks the browser's own print button?

You can see an example of my page at: http://www.minisage.com/caseygrants/pages/wwa/wwa_index.asp

NOTE: I currently have it set up so that the stylesheet_print.css is invoked any time printing occurs. I need to know how to set it up so that the stylesheet_print.css is ONLY invoked when the user clicks the 'print this page' rollover.

Thanks!
0
rascal
Asked:
rascal
  • 2
  • 2
  • 2
  • +2
3 Solutions
 
ZylochCommented:
Set up two stylesheets, one alternate and one preferred, the preferred one being the one that prints the images also.

<link rel="stylesheet"  href="default.css" title="Default">
<link rel="alternate stylesheet" href="onlyText.css" title="Text">

Then, have this function in your <head> section: (adapted from http://www.alistapart.com/articles/alternate/)

function setActiveStyleSheet(title) {
   var i, a;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1
        && a.getAttribute("title")) {
       a.disabled = true;
       if(a.getAttribute("title") == title) a.disabled = false;
     }
   }
}

When the user clicks the Print button, I would do a setTimeout of a second or so before it prints just in case. Call setActiveStyleSheet("Text"); to switch. After print is done, call setActiveStyleSheet("Default");

Regards,
${Zyloch}
0
 
COBOLdinosaurCommented:
I've never actually tried this but it might work and if it does is really easy

Do the link tag this way:

<link href="../../css/stylesheet_print.css" rel="stylesheet" type="text/css" media="projection" />


Then for the mouseover try changing the media type with

onmouseover="document.styleSheets[2].media='print'"

That is really neat if it works.  If not let me know and I will give you the hard way by hiding the style in a layer and dynamically populating it on mouseover... a lot more work.

BTW the index on the stylesheet is because stylesheets are an array and they are put into the array in listing order, so if you change the order it changes the index, but I think we can use and id to name it and index on the id.

Cd&
0
 
ZylochCommented:
If Cd's works, don't have your text formatting styles in stylesheet_print.css. Have your text formatting styles in another external stylesheet (or internal), so you'd have:

<link href="../../css/stylesheet_print.css" rel="stylesheet" type="text/css" media="projection" />
<link href="../../css/stylesheet_text.css" rel="stylesheet" type="text/css" media="projection" />

(I hope this doesn't mess up stylesheet indexing. I still have to read up on that)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ThogekCommented:
Here's another interesting thought/demo:

    <body>

    <link href="../../css/stylesheet.css" rel="stylesheet" type="text/css" media="screen"/>

    This is only a test.
    A very boring test, at that.

    <a href="#" onclick="document.getElementById('cssFile').href='../../css/stylesheet2.css';">Change</a>

    </body>
0
 
ThogekCommented:
Oops.  Change my
    <link href="../../css/stylesheet.css" rel="stylesheet" type="text/css" media="screen"/>
to
    <link id="cssFile" href="../../css/stylesheet.css" rel="stylesheet" type="text/css" media="screen" />
0
 
rascalAuthor Commented:
Thank you all for great responses! I will be trying each of them and go with what makes the best sense given our screens and users, but you've provided a great deal of information, knowledge and food for thought!
0
 
COBOLdinosaurCommented:
Glad we could help.  Thanks for the A. :^)

Cd&
0
 
RyrattCommented:
I combined a couple answers here to consense it. I checked it in Netscape and IE and it worked. You can put it in an event or a function, or whereever.


styleSheetFilename = 'file.css';

styleSheetVar = document.getElementsByTagName("link");
styleSheetVar.href = styleSheetFilename;
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now