Click to See Complete Forum and Search --> : window.close() works before window.print()


ttmtech
08-26-2003, 02:06 PM
Hi All

Below is code in my javascript function.

{

....
....

var newWindow =window.open(str,'window2', 'resizable=yes,width=750,height=500,scrollbars=yes,top=35,left=15');

newWindow.print();
newWindow.close();

}

on a print button of page I have open new window and i want to print it and close it.Now my problem is that it opens and closes it immediately,it doesn't print anything.If I make comment on newWindow.close(); then print alert popups.Is there any way I can print window and close it on print button of page.

Thanks in advance.
ttmtech

Khalid Ali
08-26-2003, 11:21 PM
try using setTimeout to delay the window.close() function call

AmirK
08-27-2003, 12:42 PM
Better than setting a time out, set the onafterprint event on the open window to close itself once it's done printing. In the open window, do this:

<script language="JavaScript" for="window" event="onafterprint">
window.close();
</script>

That'll provide the user with as much time as they like to fiddle around in the print dialog and your code should execute afterwards.

- Amir

Clarification:
The code executes after the page has been saved in the printers cache, not after the print dialog has closed.

Also, keep in mind that events are only fired on user actions - not actions taken through the code. Meaning, calling window.print() will not fire any events. If you want to fire the event manually, you'd have to call it yourself.

ttmtech
08-27-2003, 01:00 PM
Khalid and Amir


Thanks for your suggestion.setTimeout() function works fine.Amir I have tested your code but it doesn't work.Have you tested it ? then I will try it again.

once again thanks for help.


TTMTECH.

Khalid Ali
08-27-2003, 01:02 PM
Originally posted by AmirK
Better than setting a time out,
...................
<script language="JavaScript" for="window" event="onafterprint">
window.close();
</script>

- Amir

Its not better ,because what you have recomended is not in html standards,its only IE specific code.
please try not to compare things when you have no idea what you are talking about,however a simple suggestion was good enough.

AmirK
08-27-2003, 01:18 PM
Ture, it is an IE only event. However, I know that his application will be used in an IE only environment, so it's definately a cleaner approach (I've had correspondence with a cowoker of his.) An event that is triggered by the browser is more reliable than is your method, of guesstimating a random time to wait.

Don't get so defensive.

Khalid Ali
08-27-2003, 01:22 PM
Originally posted by AmirK
Don't get so defensive.

Nope not at all....just setting the record straight that your recomendation is not better at all(as a matter of fact some may consider it worse if at all, for being not a standard html recomendation)..

No worries..take care.