Page refreshes while using window.print()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • govind161986
    New Member
    • Dec 2009
    • 21

    Page refreshes while using window.print()

    Hi everyone I have stuck in a problem and need your help.

    When I print a page using javascript's window.print command the page gets refresh after printing in google chrome and FireFox.

    How to avoid the page from being refreshed?
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Post your code please.

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5390

      #3
      basicly it shouldn't ... please show the code ... just tested it with chrome and the page is not refreshed only by using window.print();

      kind regards

      PS: ahh ... a bit late :) as acoder mentioned already ... we would need to see the code :)

      Comment

      • checkerbox
        New Member
        • Nov 2011
        • 1

        #4
        Over a year and a half later, this thread still comes pretty far up on google's results for "chrome print refresh page". Since there wasn't much of an answer here, I thought I better take the time to share my luck in finding a solution to this otherwise unlucky problem.

        Here is the trick:

        onclick="window .print(); return false;"

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Thanks for posting. You're probably correct, but without knowing how window.print() is called, we can't be sure. A normal button wouldn't refresh the page. A submit button or a link (depending on the action/href) would.

          Comment

          • mendy1
            New Member
            • Aug 2016
            • 1

            #6
            Originally posted by acoder
            Thanks for posting. You're probably correct, but without knowing how window.print() is called, we can't be sure. A normal button wouldn't refresh the page. A submit button or a link (depending on the action/href) would.
            i am also having this issue

            Code:
            function printPage(){
            	document.getElementById("print").addEventListener("click", print);
            
            	function print() {
            	    var printButton = document.getElementById('print');
            	    printButton.onclick = window.print();
            	}
            }
            
            function init(){
            	printPage();
            }
            
            window.onload = init;
            Last edited by gits; Aug 9 '16, 10:39 AM. Reason: added code tags

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5390

              #7
              there might be hints already in the posts above - it will probably depend on what element the action is triggered (submit button, link or whatever) - so that information would be helpful.

              on a sidenote: the code is a bit weird - when using:
              Code:
              document.getElementById("print").addEventListener("click", print);
              you already assign the function print as the callback in case the event occurs on that element. so basically you dont need to and shouldnt reassign it with
              Code:
              printButton.onclick
              in that function again. so:
              Code:
              function print() {
                  window.print();
              }
              could be enough already. may be you should return false as well - but that depends on the element where the click is triggered - because some events still fire default actions. you can even preventDefault( ) such things.
              Last edited by gits; Aug 11 '16, 08:38 AM.

              Comment

              • Kent052194
                New Member
                • Jan 2017
                • 1

                #8
                Thanks a lot bro, this works for me

                Comment

                Working...