admin管理员组

文章数量:1391850

I am trying to set a delay between the time the pdf is opened and the print function is called on the pdf file. Currently nothing happens but if I move the print function outside of the setTimeout it will call the print function but this is not what I want because it is too quick and pdf has not been loaded. When I place alerts inside the setTimeout they are called fine with the specified 3 seconds.

var pdfWin = window.open(docPath);
setTimeout(function() {
   pdfWin.print();
},3000)

My question is: why is the print function not being called?

Thanks in advance

I am trying to set a delay between the time the pdf is opened and the print function is called on the pdf file. Currently nothing happens but if I move the print function outside of the setTimeout it will call the print function but this is not what I want because it is too quick and pdf has not been loaded. When I place alerts inside the setTimeout they are called fine with the specified 3 seconds.

var pdfWin = window.open(docPath);
setTimeout(function() {
   pdfWin.print();
},3000)

My question is: why is the print function not being called?

Thanks in advance

Share Improve this question asked Nov 20, 2012 at 0:09 sam.tldrsam.tldr 653 silver badges9 bronze badges 2
  • 4 You shouldn't use timeouts for that purpose. You can't possibly predict the time it will take for a file to be downloaded because the internet speed of the users and the server's response time will vary. You should use an event handler that fires when the file is downloaded. – JCOC611 Commented Nov 20, 2012 at 0:11
  • I understand this and it is not the final solution. I just want to see the output before I move on to something else – sam.tldr Commented Nov 20, 2012 at 0:13
Add a ment  | 

1 Answer 1

Reset to default 6
var pdfWin = window.open(docPath);
pdfWin.onload = function() {
    pdfWin.print();
};

Works fine for me in chrome.

Due to same-origin policy, you can only call .print() on the window if it resides on the same domain as the parent window. Otherwise I could just open up your facebook on my page and submit some forms there ;p

本文标签: Javascript print using setTimeoutStack Overflow