printing - How can one detect via Javascript if a print stylesheet is in effect? - Stack Overflow

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

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 puted style.

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

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 puted style.

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

Share asked Dec 5, 2008 at 20:11 jschrabjschrab 11k4 gold badges22 silver badges17 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

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.

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./articles/alternate/.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744652241a4585970.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信