This JS works okay on Chrome but not on Firefox, new window cannot be closed from JS:
function print(){
var canvas = document.getElementById("canvas");
var printWindow = window.open();
var printHtml = "<!DOCTYPE html>"
+ "<html lang='en'>"
+ "<meta charset='UTF-8'>"
+ "<title>Printing the canvas</title>"
+ "<style type='text/css' media='print'>"
+ "@page{size:auto; margin:0mm auto 0mm auto;} h1{text-align:center; font:10mm Arial, sans-serif;}"
+ "</style>"
+ "<script>"
+ "window.onafterprint = function(){ window.close(); };"
+ "</script>"
+ "<body>"
+ "<br><h1>My canvas</h1>"
+ "<br><img onload='print();' src='"+canvas.toDataURL()+"'/>"
+ "</body>"
+ "</html>";
printWindow.document.write(printHtml);
}
Firefox supposedly allows window.close() on windows which were opened by JS but this does not work for me.
2
Answers
you have to use
Window.postMessage()
event like this:}
With a small variation it works in firefox