views:

1046

answers:

2

I would like to have alternate behavior during a print stylesheet on a web page. Something along the lines of:

If this page is being printed, don't bother calling SWFObject to summon an .swf into existence. Just leave the HTML that the Flash will replace.

I've tried things like setting a known element to a known style that exists for the screen but not for the print stylesheet. But getting a "style" via Javascript doesn't get a computed style.

Summary: In a cross-browser way, is it possible to tell which stylesheet is in effect?

+5  A: 

It sounds like you're confused that print style-sheets are used when you view a printer-friendly page, but that is not the case. A print style sheet isn't applied until the user actually sends the page to the printer. At this point, any javascript that is going to run has already finished.

What you want to do is put your SWFObject inside a div container, and have the container styled as display:none; for the print media.

Joel Coehoorn
A: 

You could use JavaScript to access the stylesheets in the document and then check if the 'Print' stylesheet is active. Once you determined which CSS is active then you could manage your content.

The getActiveStyleSheet function would looks something like this:

function getActiveStyleSheet() 
{
    var i, a;

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

    return null;
}

You can find the code here: http://www.alistapart.com/articles/alternate/.

Victor
This solution is ... only partial. Without the rest of the "cookie system", as described at http://www.alistapart.com/articles/alternate/ it won't work.
I had this snippet saved on my machine, but couldnt remember where i had gotten it from.
Victor