[thelist] Disable CSS before calling window.print()

Lee Kowalkowski lee.kowalkowski at googlemail.com
Thu Jun 5 10:12:59 CDT 2008


On 05/06/2008, Bill Moseley <moseley at hank.org> wrote:
> So, I was requested to add a print link that not only calls
> window.print() but first disables all media="screen" style sheets
> so that the layout in the browser looks like what the printed page
> will look like (kind of a Print Preview function but can't interact
> with it).
>
>    function print_preview() {
>        for(var i=0; i < document.styleSheets.length;i++) {
>            var sheet = document.styleSheets.item(i);
>
>            if ( sheet.media.mediaText == 'screen' )
>                sheet.disabled = true;
>        }
>
>        window.print();
>
>        for(var i=0; i < document.styleSheets.length;i++) {
>            var sheet = document.styleSheets.item(i);
>
>            if ( sheet.media.mediaText == 'screen' )
>                sheet.disabled = false;
>        }
>
>        return false;
>    }
>
> Suggestions for a better approach -- or reasons why it's not disabling
> the styles in IE?

Look:
<script>
print();
alert("Print");
</script>

IE treats print() as asynchronous, Firefox does not.  In IE you can
carry on browsing before commencing with the print dialog, it will
still print the page you invoked print on (so your requirement can get
a little lost there anyway).  Perhaps you should go for a print
preview link for this requirement.

Internet Explorer has onbeforeprint and onafterprint events.  But the
changes will only be visible whilst the page is being "prepared for
printing" which is before the dialog appears, so the changes won't be
visible for long enough for the user to make a decision.

Never mind...

-- 
Lee



More information about the thelist mailing list