SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Oct 2005
    Posts
    288
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Detecting file->print click

    My css stylesheet distinguishes between media print and screen. I'd like my javascript to do the same as I have a collapsable address book. To wit: For the screen my javascript is:
    Code:
    <!--
     window.onload=addressbook;
     function addressbook(id) {
      var d = document.getElementById(id);
      for (var i = 1; i<=15 ; i++) {
       if (document.getElementById('saddressbook'+i)) {
        document.getElementById('saddressbook'+i).style.display='none';
       }
      }
      if (d) {d.style.display='block';}
     }
     // -->
    For the print, I would want all displays. That is:
    Code:
    <!--
     function printaddressbook() {
      var d = document.getElementById(id);
      for (var i = 1; i<=15 ; i++) {
       if (document.getElementById('saddressbook'+i)) {
        document.getElementById('saddressbook'+i).style.display='block';
       }
      }
     }
     // -->
    Is there a way that I can have the javascript detect a CTRL-P or a click on the browser's File->Print to fire this printaddressbook function, or do I have to create a "print-view" button?

    Regards,

    Grandpa Brian

  2. #2
    SitePoint Addict ALL's Avatar
    Join Date
    Oct 2005
    Location
    South Dakota
    Posts
    215
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    no, you cannot have javascript detect a file>print but you should beable to detect a ctrl+p

    You will need to go with a print-view link... if you really want to get fancy, have it create a popup window, and in that popup window have the printable version, then have javascript display the print dialog box.

    The reason you cant detect a print is because of security.

    -ALL
    Did I help you?
    You can repay me, support one of my projects (no money needed):
    JavaScript Wiki, Another Web Forum, Paranormal Site

  3. #3
    SitePoint Addict
    Join Date
    Oct 2005
    Posts
    288
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The reason you cant detect a print is because of security
    .
    Hm-m-m. But it is not a security issue for CSS (media=print).

    Thanks for saving me a frustrating effort.

    Regards,

    Grandpa

  4. #4
    SitePoint Addict ALL's Avatar
    Join Date
    Oct 2005
    Location
    South Dakota
    Posts
    215
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    yes, but that is about as far as it goes... However a non-cross-browser way of doing it is: onbeforeprint() set to the window object... for example:

    Code:
    window.onbeforeprint=Function("doStuffBeforePrint()");
    but i seggest using a print css because it is the most universal way of doing it,
    -ALL
    Did I help you?
    You can repay me, support one of my projects (no money needed):
    JavaScript Wiki, Another Web Forum, Paranormal Site


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
  •