javascript beforeprint not working in mozila

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maminx
    New Member
    • Jul 2008
    • 77

    javascript beforeprint not working in mozila

    hello all. i have html page to be printed out..it's generated by the button print like below

    <input type="button" name="btn_print " value="Print Invoice" onClick="javasc ript:window.pri nt();">

    of course, the button will shown to the page when it's printed..
    but i don't want that button shown when user print the page (paper) or when the user commands "print preview" in the browser...

    Than, i already solve the request with this function below..

    Code:
    <script type="text/javascript">
    // Hide tags with id="noprint" when printing
    function DoNotPrint()
        {
      	  var a = document.all.item("noprint");
          if (a!=null) {
                if (a.length!=null) {
                //multiple tags found
                    for (i=0; i < a.length; i++) {
                    a(i).style.display = window.event.type == "beforeprint" ? "none" :"inline";
                }
            } else 
            //only one tag
            a.style.display = window.event.type == "beforeprint" ? "none" :"inline";
        }
    }
    </script>
    and the body HTML is like below :

    <body onafterprint="j avascript:DoNot Print();"onbefo reprint="javasc ript:DoNotPrint ();">

    so, when user commands print preview or print the page, the button "btn_print" above, will not shown..

    the problem is, that's works only in IE, doesn't work in mozilla...

    so, any idea???

    thanks before

    regards, maminx
  • maminx
    New Member
    • Jul 2008
    • 77

    #2
    sorry forgot the HTML code rules..here i repost my question

    hello all. i have html page to be printed out..it's generated by the button print like below

    [HTML]<input type="button" name="btn_print " value="Print Invoice" onClick="javasc ript:window.pri nt();">[/HTML]

    of course, the button will shown to the page when it's printed..
    but i don't want that button shown when user print the page (paper) or when the user commands "print preview" in the browser...

    Than, i already solve the request with this function below..

    Code:
    <script type="text/javascript">
    // Hide tags with id="noprint" when printing
    function DoNotPrint()
        {
      	  var a = document.all.item("noprint");
          if (a!=null) {
                if (a.length!=null) {
                //multiple tags found
                    for (i=0; i < a.length; i++) {
                    a(i).style.display = window.event.type == "beforeprint" ? "none" :"inline";
                }
            } else 
            //only one tag
            a.style.display = window.event.type == "beforeprint" ? "none" :"inline";
        }
    }
    </script>
    and the body HTML is like below :

    [HTML]<body onafterprint="j avascript:DoNot Print();"onbefo reprint="javasc ript:DoNotPrint ();">[/HTML]

    so, when user commands print preview or print the page, the button "btn_print" above, will not shown..

    the problem is, that's works only in IE, doesn't work in mozilla...

    so, any idea???

    thanks before

    regards, maminx

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Instead of using JavaScript, use a print stylesheet to hide the button. See Going to print.

      Comment

      • maminx
        New Member
        • Jul 2008
        • 77

        #4
        i've already solve the problem.
        Last edited by acoder; Dec 16 '08, 08:50 AM. Reason: Removed link to competing forum

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          That's good to see. For something like this, CSS is the way to go.

          Comment

          Working...