hiding, showing and printing in javascript

Discussion in 'ASP .Net' started by Mike P, Jul 24, 2007.

  1. Mike P

    Mike P Guest

    I am trying to print a webpage and making some objects invisible for the
    print, and then visible again after. But my code does not work :

    function PrintInvoice()
    {
    document.getElementById("Button1").style.visibility = "hidden";
    document.getElementById("Button2").style.visibility = "hidden";

    window.print();

    document.getElementById("Button1").style.visibility = "visible";
    document.getElementById("Button2").style.visibility = "visible";
    }

    Can anybody with good javascript skills help?



    *** Sent via Developersdex http://www.developersdex.com ***
     
    Mike P, Jul 24, 2007
    #1
    1. Advertising

  2. rather than iterating every control on the page that should not be printed,
    it is advised to use a separate stylesheet (or style rules) for printing in
    which you define certain types with display:none. See
    http://www.w3.org/TR/REC-CSS2/media.html for more info

    "Mike P" wrote:

    > I am trying to print a webpage and making some objects invisible for the
    > print, and then visible again after. But my code does not work :
    >
    > function PrintInvoice()
    > {
    > document.getElementById("Button1").style.visibility = "hidden";
    > document.getElementById("Button2").style.visibility = "hidden";
    >
    > window.print();
    >
    > document.getElementById("Button1").style.visibility = "visible";
    > document.getElementById("Button2").style.visibility = "visible";
    > }
    >
    > Can anybody with good javascript skills help?
    >
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    >
     
    =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?=, Jul 24, 2007
    #2
    1. Advertising

  3. Mike P

    Mike P Guest

    Found the error - it was simply a matter of calling the function without
    braces i.e. PrintInvoice instead of PrintInvoice().



    *** Sent via Developersdex http://www.developersdex.com ***
     
    Mike P, Jul 24, 2007
    #3
  4. There are two javascripts events onbeforeprint and onafterprint that are
    good for placing this type of code in. It will make sure that the page
    prints correctly regardless of the way how you initiate print, even if the
    user clicks the browser's Print button.

    And the css idea that the other poster suggested is even better.

    --
    Eliyahu Goldin,
    Software Developer
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    "Mike P" <> wrote in message
    news:...
    >I am trying to print a webpage and making some objects invisible for the
    > print, and then visible again after. But my code does not work :
    >
    > function PrintInvoice()
    > {
    > document.getElementById("Button1").style.visibility = "hidden";
    > document.getElementById("Button2").style.visibility = "hidden";
    >
    > window.print();
    >
    > document.getElementById("Button1").style.visibility = "visible";
    > document.getElementById("Button2").style.visibility = "visible";
    > }
    >
    > Can anybody with good javascript skills help?
    >
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
     
    Eliyahu Goldin, Jul 25, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Kraft
    Replies:
    1
    Views:
    415
    S. Justin Gengo
    Aug 15, 2003
  2. Amber
    Replies:
    1
    Views:
    639
    Alvin Bruney
    Oct 11, 2003
  3. Replies:
    6
    Views:
    6,435
    Wanda Li
    Mar 18, 2005
  4. Jonathan Wood

    Hiding/Showing Elements with javascript

    Jonathan Wood, Mar 18, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    303
    Mark Rae [MVP]
    Mar 18, 2008
  5. Ste
    Replies:
    41
    Views:
    862
    Thomas 'PointedEars' Lahn
    Aug 1, 2007
Loading...

Share This Page