JavaScript / jQuery - New Window + Print Page on Ajax callback / repsonse

HTML

<button id="createPDF">
  PDF print
</button>

JavaScript

function test() {
 alert('yes');
}

$('#createPDF').on('click',function() {
	//var $trigger = $(this);

 // $.get('/echo/js/?js=hello%20world', function(data) {
  		var newWindow = window.open("","_blank");
			$(newWindow.document.body).html('test');
      var ele = document.createElement('div');
      ele.innerHTML = 'new element';
      $(newWindow.document.body).append(ele);
      
      var script = document.createElement('script');
      script.innerHTML = 'test()';
      
       $(newWindow.document.body).append(script);
             
      //newWindow.print();
      newWindow.close();
  //});
  
  return false;
});