Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to swap CSS files at runtime?

Posted on 2004-08-02
8
546 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Capture logon name 13 86
Border and image sizing 5 28
Bottom border change 5 17
CSS issue in Nested Master Pages 9 26
Unordered lists (HTML ul tags) are very handy to create menus of all types, shapes and sizes.  All it takes is a little knowledge of CSS and the possibilities are endless. First off, you should know the general format for menus created using the …
When applying CSS to your HTML, there are many different ways to select which element(s) the CSS applies to.  Some of these selectors are more commonly known and used than others - Here are the more common ones: #X - Matches an ID of X .X - Matche…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
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 …

808 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