I'm trying to open the print dialog when a page is done loading. Using the code below, I get no errors and the window opens, but the onload never seems to fire and the print dialog does not open (Firefox and Chrome -- haven't tested other browsers).
I've tried these :
var printWindow = window.open(result.url);
printWindow.window.onload = function () {
printWindow.window.print();
console.log("Printing page");
}
and
var printWindow = window.open(result.url);
printWindow.onload = function () {
printWindow.print();
console.log("Printing page");
}
and
var printWindow = window.open(result.url);
printWindow.addEventListener("load", function() {
printWindow.print();
console.log("Got here");
});
An example of what the result.url looks like:
I'm trying to see if this post applies to my case, and how I can use the answer to my advantge. My original window that allows the user to generate the new url is at http://ourserver/ourapp whereas the new url is at http://ourerver/arcgis/... I don't believe this is the same situation, but I am trying to see how it is possible to exploit the answer of the suggested duplicate if this is the case.
UPDATE So I finally got the signal to fire using a postMessage approach. However, I think i'm running into a window.print() specific problem, because the print statement comes up to the console, but I still have the following issues:
- On chrome, no print dialog opens
On Firefox, print dialog opens, but it prints about:blank, and not the url I'd opened the window with...seems like the page hadn't loaded completely before the dialog opened or something (same thing that happens when I call printWindow.print() directly without a handler).
var printWindow = window.open(result.url); var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent"; var eventer = window[eventMethod]; var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message"; // Listen to message from child window eventer(messageEvent,function(e) { console.log('origin: ', e.origin) // Check if origin is proper if( e.origin != 'http://ourserver' ){ return } console.log('parent received message!: ', e.data); try { //attempt to deserialize function and execute as closure eval('(' + decodeURI(e.data) + ')();'); } catch(e) {} }, false); printWindow.opener.postMessage({serializeFunction(openPrintDialog)}, 'http://ourserver');
The code references these functions:
function serializeFunction(f) {
return encodeURI(f.toString());
}
function openPrintDialog() {
printWindow.print();
console.log("Got here");
}