最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论