Solved

How to swap CSS files at runtime?

Posted on 2004-08-02
8
543 Views
Last Modified: 2008-03-03
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
Comment
Question by:rascal
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 36

Accepted Solution

by:
Zyloch earned 34 total points
ID: 11699124
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
 
LVL 53

Assisted Solution

by:COBOLdinosaur
COBOLdinosaur earned 33 total points
ID: 11699130
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
 
LVL 36

Expert Comment

by:Zyloch
ID: 11699175
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
 
LVL 15

Expert Comment

by:Thogek
ID: 11707531
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 15

Assisted Solution

by:Thogek
Thogek earned 33 total points
ID: 11707542
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
 
LVL 1

Author Comment

by:rascal
ID: 11707576
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
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 11707693
Glad we could help.  Thanks for the A. :^)

Cd&
0
 
LVL 1

Expert Comment

by:Ryratt
ID: 15083117
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Creating a CSS block that only applies to printing By default, all of your CSS applies to every possible view of your page - whether on screen, printed, landscape, touch-screen, or whatever.  You can, however, add CSS that only applies under certai…
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to style different bullet points for unordered lists in CSS. Begin with a normal unordered list; the default bullet point is a solid circle: In the CSS, create a defined class of unordered list by typing "ul.S…
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now