我試圖立即打印到客戶端的打印機在頁面加載,使用客戶端執行以下操作:打印對window.load
function openWin() {
var printWindow = window.open();
printWindow.document.write('<html><head><title>Test Page</title>');
printWindow.document.write('</head><body>');
printWindow.document.write("Name: <?php echo $firstname . " " . $lastname; ?><br />Spouse: <?php echo $sfirstname . " " . $slastname; ?><br />Address: <?php echo $_POST['address'] ?><br />City/State/Zip: <?php echo $_POST['city'] . ", " . $_POST['state'] . " " . $_POST['zip'] ?><br />");
printWindow.document.write('</body></html>');
printWindow.document.close();
printWindow.focus();
printWindow.print();
printWindow.close();
}
要獲得功能火,我使用
$(window).load(function(){
openWin();
});
當文檔打開時,什麼都不打印 - 即openWin()
函數不會觸發。我試着爲它創建一個按鈕,並在窗口加載時使用觸發器來點擊按鈕。我也嘗試setTimeout()
延遲一段時間,以便頁面可以繼續加載等。沒有任何工作。
我在控制檯收到此錯誤:
application2.php:240 Uncaught TypeError: Cannot read property 'document' of null
但是,當我乾脆在控制檯上輸入openWin()
,打印頁面正常打開。
任何幫助,將不勝感激。
謝謝。你說得對。但是昨天,我放棄了這個想法,併爲用戶添加了一個「強制性」打印按鈕,從而實現瞭解決方法。 – Mfffffffffffff