0

I have a question regarding a javascript command. What I am doing is on a asp.net page, a user clicks on a print button which I have another page open up and at the bottom of that page, I put in a simple script command, but I have noticed that my window.close doesnt always work. Here is my code below. Any help would be very much appreciated.

<script type="text/javascript">
               window.print();
               window.close();
</script>
0

2 Answers 2

1

Try adding a delay before calling window.close()

<script type="text/javascript">
    window.print();
    setTimeout(window.close, 10000);
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry for saying this, but that is not a good solution, because it will have different effects in different browsers. In IE for example the "countdown" starts when the print-dialog closes, in Chrome immediately because Chrome doesn't open a modal dialog for printing.
0

I don't think you will get it to work this way. For example in IE the print function will prevent the close function and in many versions of Chrome window.close doesn't work.

There are 2 other ways of doing this:

  1. If possible try to use a print style-sheet by specifying an own stylesheet with media="print". In this stylesheet you can hide things such as navigation menus and choose appropriate colors for printing. details on selfhtml
  2. If that does not work let the user understand what you are doing: rename your current print-button to "print preview" and let the user close this window when he is done printing. I have seen many other websites do it this way too. You can still call window.print if you like.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.