How can one detect via Javascript if a print stylesheet is valid?

I would like to have an alternative behavior during the print stylesheet on a web page. Something like:

If this page prints, don't worry about calling SWFObject to call the .swf into existence. Just leave the HTML, which Flash will replace.

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

Synopsis: Cross-browser can tell which stylesheet is in effect?

+1


source to share


2 answers


It sounds like you are confused about the print styles being used when viewing a print friendly page, but you are not. The print style sheet is not applied until the user sends the page to the printer. At this point, any javascript that will run is already completed.



What you want to do is put your SWFObject inside a container div and create a container in a display:none;

printable view .

+6


source


You can use JavaScript to access style sheets in a document and then check if the Print style sheet is active. Once you have determined which CSS is active, you can manage your content.

The function getActiveStyleSheet

will look 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;
}

      

The code can be found here: http://www.alistapart.com/articles/alternate/ .

+1


source







All Articles