0

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:

http://ourserver/arcgis/rest/directories/arcgisoutput/OurTools/Print_Tool_Adv_GPServer/_ags_WebMap_de458080-c19a-11e6-8ade-005056a65a05.PDF

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");
  }
Community
  • 1
  • 1
user25976
  • 1,005
  • 4
  • 18
  • 39
  • 2
    Possible duplicate of [Detecting the onload event of a window opened with window.open](http://stackoverflow.com/questions/3030859/detecting-the-onload-event-of-a-window-opened-with-window-open) – Scott Marcus Dec 14 '16 at 02:00
  • Unrelated to your question, but I hate sites that force a print dialog on me. Do you really need this functionality? – Kijewski Dec 14 '16 at 02:47
  • @Kay Users are asking for it, so I'm doing it...Plus, it's a printing widget, so it makes sense in the context. – user25976 Dec 14 '16 at 02:49

0 Answers0