title: javascript: Print text that is received via ajax
body: <p>This is what I want to do:</p>
  
  <ol>
  <li>User clicks <strong>print</strong> button;</li>
  <li>This calls function, which does ajax call to get the text to be printed;</li>
  <li>A new window is opened and the text is written to this.</li>
  </ol>
  
  <p>The window and print is handled like this:</p>
  
  <pre><code> my_text = "hello";
  
   newWin= window.open();
   newWin.document.write(my_text);
   newWin.document.close();
   newWin.focus();
   newWin.print();
   newWin.close();
  </code></pre>
  
  <p>This works fine. My issue is how to get <code>my_text</code>. I have tried to put the above code inside an ajax call:</p>
  
  <pre><code>$.ajax({
          type: "GET", url: the_url, data: {},
          success: function(data){
               newWin= window.open();
               newWin.document.write(data);
               newWin.document.close();
               newWin.focus();
               newWin.print();
               newWin.close();
          }
          ,error: function() {
          } 
       }); 
  </code></pre>
  
  <p>However, this causes the new window to be seen as a pop-up and it's caught by the pop-up blocker. If I choose to see the pop-up message then it has correctly filled in the text. I tried opening the window first, but then nothing gets written to it.</p>