SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard rozner's Avatar
    Join Date
    Oct 2002
    Location
    Paris
    Posts
    1,058
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    printer friendly page

    Hi,

    I was trying to make a printer friendly page, I've gone through several possibilities. At the moment I'm trying to just change the CSS, which sort of works, but there is a problem. When the user clicks the print link, it hides a bunch of stuff that I don't want printed, then in the JS I call window.print(), and then I show all the elements again. This way it still prints everything. If I comment out everything after the print call it prints fine. Is there a way to delay the rest of the function until the page is sent to the printer? The only thing that came to mind is setTimeout but this is a horrible idea! This is the function I'm using.

    Code JavaScript:
    function openPrinterFriendly() {
     
        hideElement('mediabar');
        hideElement('mainTopicsContainerDiv');
        hideElement('mainSearchContainerDiv');
        hideElement('topThumbnails');
     
        var topics = getObject('mainTopicsContainer');
        var wt = topics.style.width;
        topics.style.width = '0px';
     
        var search = getObject('mainSearchContainer');
        var ws = search.style.width;
        search.style.width = '0px';
     
        document.body.style.backgroundImage = '';
     
        window.print();
     
    // if I comment the rest out, it works fine, but then I'll require another click  from the user to get the site back to normal
        document.body.style.backgroundImage = 'url(http://www.site.com/images/mediabar/topmenbg.gif)';
     
        topics.style.width = wt;
        search.style.width = ws;
     
        showElement('mediabar');
        showElement('mainTopicsContainerDiv');
        showElement('mainSearchContainerDiv');
        showElement('topThumbnails');
    }

    Thanks

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not just have a print style sheet (media="print") or print-specific rules in an @media at-rule?

    Then all you'd have to do is window.print().
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Wizard rozner's Avatar
    Join Date
    Oct 2002
    Location
    Paris
    Posts
    1,058
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well that worked, learning something new every day, thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •