1

I'm generating a printer-friendly page that automatically opens the browser's print dialog box upon user visit. Is there a way to close the window if the user presses "Cancel"?

kevi kevi
  • 157
  • 1
  • 6
  • 12
  • 1
    is the cancel button you are referring is at the browser's print dialog box? showing the available printer.. something like that? – Netorica May 13 '12 at 04:36
  • @kevi kevi - JavaScript can't detect if user clicked on `cancel` or not. – Derek 朕會功夫 May 13 '12 at 04:45
  • possible duplicate of [How to close current tab in a browser window?](http://stackoverflow.com/questions/2076299/how-to-close-current-tab-in-a-browser-window) – Naveed May 13 '12 at 05:08

2 Answers2

1
window.close()

closes the current window

you can do like.

<button type="button" id="cancelButton">Cancel</button>

$('#cancelButton').click(function(){
    window.close();
});
Ibrahim Azhar Armar
  • 25,288
  • 35
  • 131
  • 207
  • And.... How do I detect if the user clicked the cancel button on the print dialog box? – kevi kevi May 13 '12 at 04:30
  • 3
    @IbrahimAzharArmar I believe he's referring to the Cancel button in the OS-level printer dialog window, not a DOM element. – cheeken May 13 '12 at 04:36
  • @cheeken the question is kinda out of line for js.. is the browser have event handlers of OS specific dialog boxes like the print dialog box? – Netorica May 13 '12 at 04:42
0

window.print() has no standards; its operation is a grab-bag from browser to browser. Futhermore, to my knowledge, in no browser implementation does it pass information back. You cannot determine how the user responded to the print dialog via JavaScript.

That said, it would seem the only point of the print-friendly page would be to print. Perhaps you can close the window after prompting to print, regardless of how the user responded?

cheeken
  • 33,663
  • 4
  • 35
  • 42