ユーザーがボタンをクリックすると、タブが開きます。でonload
、私はそれは、印刷ダイアログを表示していますが、ユーザーはそれをタブ自体を閉じることができれば、それはプリンタに送信した後は、印刷することが可能であったかどうか私に尋ねました。これができるかどうかはわかりません。を使用してみましたsetTimeout();
が、ユーザーが気を散らしてタブを再度開く必要があるため、定義された期間ではありません。これを達成する方法はありますか?
ユーザーがボタンをクリックすると、タブが開きます。でonload
、私はそれは、印刷ダイアログを表示していますが、ユーザーはそれをタブ自体を閉じることができれば、それはプリンタに送信した後は、印刷することが可能であったかどうか私に尋ねました。これができるかどうかはわかりません。を使用してみましたsetTimeout();
が、ユーザーが気を散らしてタブを再度開く必要があるため、定義された期間ではありません。これを達成する方法はありますか?
回答:
print()呼び出しの直後にウィンドウを閉じようとすると、すぐにウィンドウが閉じて、print()が機能しなくなる可能性があります。これはあなたがすべきではないことです:
window.open();
...
window.print();
window.close();
このソリューションはFirefoxで機能します。これは、print()呼び出しで、印刷が完了するまで待機してから、javascriptの処理を続行してウィンドウをclose()するためです。IEは、print()呼び出しが行われるのを待たずに、close()関数を呼び出すため、これで失敗します。印刷が完了する前に、ポップアップウィンドウが閉じます。
これを解決する1つの方法は、「onafterprint」イベントを使用することですが、これらのイベントはIEでのみ機能するため、お勧めしません。
最良の方法は、印刷ダイアログが閉じられたら(印刷が完了またはキャンセルされたら)ポップアップウィンドウを閉じることです。この時点で、ポップアップウィンドウにフォーカスがあり、「onfocus」イベントを使用してポップアップを閉じることができます。
これを行うには、ポップアップウィンドウに次のJavaScript埋め込みコードを挿入するだけです。
<script type="text/javascript">
window.print();
window.onfocus=function(){ window.close();}
</script>
このheplsを願っています;-)
更新:
新しいChromeブラウザについては、まだすぐに閉じる可能性があります。こちらをご覧ください。私はこの変更を実装しました、そしてそれは現在のすべてのブラウザで動作します:2/29/16
setTimeout(function () { window.print(); }, 500);
window.onfocus = function () { setTimeout(function () { window.close(); }, 500); }
window.onload = function () { window.print(); setTimeout(window.close, 500); };
これが私が思いついたものです。なぜ閉じるまでに少し遅れがあるのかわかりません。
window.print();
setTimeout(window.close, 0);
ただ:
window.print();
window.close();
できます。
Scripts may close only the windows that were opened by it.
警告を忘れないでください。
window.onafterprint = function(){ window.close()};
私は自分がしたことと自分のために働いたことを書きたいだけです(私が試した他の何もうまくいかなかったので)。
印刷ダイアログが表示される前にIEがウィンドウを閉じるという問題がありました。
多くの試行錯誤のテストの後、これは私が仕事に取り掛かったものです:
var w = window.open();
w.document.write($('#data').html()); //only part of the page to print, using jquery
w.document.close(); //this seems to be the thing doing the trick
w.focus();
w.print();
w.close();
これはすべてのブラウザで機能するようです。
このコードは私にとって完璧に機能しました:
<body onload="window.print()" onfocus="window.close()">
ページが開くと、自動的に印刷ダイアログが開き、印刷またはキャンセルするとウィンドウが閉じます。
それが役に立てば幸い、
これを行うことで、これは簡単に解決できます。
<script type="text/javascript">
window.print();
window.onafterprint = window.close();
</script>
または、たとえば次のようなことをしたい場合は、前のページに移動してください。
<script type="text/javascript">
window.print();
window.onafterprint = back;
function back() {
window.history.back();
}
</script>
window.onafterprint = window.close();
あるべきwindow.onafterprint = window.close;
旧割り当て結果のwindow.close()
へonafterprint
。これによりwindow.close()
、ハンドラーが設定されるとすぐに実行され、onafterprint
イベントが発生すると何も実行されません。後者window.close
は、イベントハンドラーとして割り当てます。これが必要なものです。
window.print();
は、印刷ダイアログが閉じられるまでコードの実行が一時停止します。これが発生すると、割り当てonafterprint
られる前にイベントが発生する可能性がありonafterprint
ます。したがって、window.onafterprint = window.close;
前に来る方が良いでしょうwindow.print();
。それ以外の場合、この回答で使用されているアプローチは、他の回答で提案されているonfocusまたはsetTimeoutベースのアプローチよりもうまく機能します。
これはすでにテストされたクロスブラウザソリューションです 2016/05までにChrome、Firefox、Operaでれです。
ことを念頭に置いてくださいマイクロソフトエッジはバグがあるプリントがキャンセルされた場合は、ウィンドウを閉じません。関連リンク
var url = 'http://...';
var printWindow = window.open(url, '_blank');
printWindow.onload = function() {
var isIE = /(MSIE|Trident\/|Edge\/)/i.test(navigator.userAgent);
if (isIE) {
printWindow.print();
setTimeout(function () { printWindow.close(); }, 100);
} else {
setTimeout(function () {
printWindow.print();
var ival = setInterval(function() {
printWindow.close();
clearInterval(ival);
}, 200);
}, 500);
}
}
クロームIを使用すると、取得するためにしばらくの間しようとしたwindow.onfocus=function() { window.close(); }
と
<body ... onfocus="window.close()">
の仕事に。私の結果:
また<body onload="window.print(); window.close()" >
、印刷ダイアログで何かをクリックする前にウィンドウが閉じてしまうことも試しました。
どちらも使えませんでした。そこで、小さなJqueryを使用してドキュメントのステータスを監視しましたが、このコードは機能します。
<script type="text/javascript">
var document_focus = false; // var we use to monitor document focused status.
// Now our event handlers.
$(document).focus(function() { document_focus = true; });
$(document).ready(function() { window.print(); });
setInterval(function() { if (document_focus === true) { window.close(); } }, 500);
</script>
jqueryが含まれていることを確認してから、これをコピーして、印刷するhtmlに貼り付けてください。ユーザーが印刷、PDFとして保存、または印刷ジョブをキャンセルした場合、ウィンドウ/タブは自動的に自己破壊します。注:私はこれをクロムでのみテストしました。
ジプシーがコメントで指摘したように、ドキュメントのフォーカスステータスは必要ありません。noamtcohenからの回答を使用するだけで、コードをそれに変更して機能します。
以下は私のために働いた:
function print_link(link) {
var mywindow = window.open(link, 'title', 'height=500,width=500');
mywindow.onload = function() { mywindow.print(); mywindow.close(); }
}
onafterprintイベントハンドラーでwindow.closeをラップするだけで、うまくいきました
printWindow.print();
printWindow.onafterprint = () => printWindow.close();
次のソリューションは、2014-03-10の時点で、IE9、IE8、Chrome、およびFFの新しいバージョンで機能しています。シナリオは次のとおりです。ウィンドウ(A)で、ボタン/リンクをクリックして印刷プロセスを開始すると、印刷するコンテンツを含む新しいウィンドウ(B)が開き、印刷ダイアログがすぐに表示されます。キャンセルまたは印刷すると、新しいウィンドウ(B)が自動的に閉じます。
次のコードはこれを許可します。このjavascriptコードは、ウィンドウA(ウィンドウBではない)のhtmlに配置されます。
/**
* Opens a new window for the given URL, to print its contents. Then closes the window.
*/
function openPrintWindow(url, name, specs) {
var printWindow = window.open(url, name, specs);
var printAndClose = function() {
if (printWindow.document.readyState == 'complete') {
clearInterval(sched);
printWindow.print();
printWindow.close();
}
}
var sched = setInterval(printAndClose, 200);
};
プロセスを起動するためのボタン/リンクは、次のように、この関数を呼び出すだけです。
openPrintWindow('http://www.google.com', 'windowTitle', 'width=820,height=600');
<!doctype html>
<html>
<script>
window.print();
</script>
<?php
date_default_timezone_set('Asia/Kolkata');
include 'db.php';
$tot=0;
$id=$_GET['id'];
$sqlinv="SELECT * FROM `sellform` WHERE `id`='$id' ";
$resinv=mysqli_query($conn,$sqlinv);
$rowinv=mysqli_fetch_array($resinv);
?>
<table width="100%">
<tr>
<td style='text-align:center;font-sie:1px'>Veg/NonVeg</td>
</tr>
<tr>
<th style='text-align:center;font-sie:4px'><b>HARYALI<b></th>
</tr>
<tr>
<td style='text-align:center;font-sie:1px'>Ac/NonAC</td>
</tr>
<tr>
<td style='text-align:center;font-sie:1px'>B S Yedurappa Marg,Near Junne Belgaon Naka,P B Road,Belgaum - 590003</td>
</tr>
</table>
<br>
<table width="100%">
<tr>
<td style='text-align:center;font-sie:1'>-----------------------------------------------</td>
</tr>
</table>
<table width="100%" cellspacing='6' cellpadding='0'>
<tr>
<th style='text-align:center;font-sie:1px'>ITEM</th>
<th style='text-align:center;font-sie:1px'>QTY</th>
<th style='text-align:center;font-sie:1px'>RATE</th>
<th style='text-align:center;font-sie:1px'>PRICE</th>
<th style='text-align:center;font-sie:1px' >TOTAL</th>
</tr>
<?php
$sqlitems="SELECT * FROM `sellitems` WHERE `invoice`='$rowinv[0]'";
$resitems=mysqli_query($conn,$sqlitems);
while($rowitems=mysqli_fetch_array($resitems)){
$sqlitems1="SELECT iname FROM `itemmaster` where icode='$rowitems[2]'";
$resitems1=mysqli_query($conn,$sqlitems1);
$rowitems1=mysqli_fetch_array($resitems1);
echo "<tr>
<td style='text-align:center;font-sie:3px' >$rowitems1[0]</td>
<td style='text-align:center;font-sie:3px' >$rowitems[5]</td>
<td style='text-align:center;font-sie:3px' >".number_format($rowitems[4],2)."</td>
<td style='text-align:center;font-sie:3px' >".number_format($rowitems[6],2)."</td>
<td style='text-align:center;font-sie:3px' >".number_format($rowitems[7],2)."</td>
</tr>";
$tot=$tot+$rowitems[7];
}
echo "<tr>
<th style='text-align:right;font-sie:1px' colspan='4'>GRAND TOTAL</th>
<th style='text-align:center;font-sie:1px' >".number_format($tot,2)."</th>
</tr>";
?>
</table>
<table width="100%">
<tr>
<td style='text-align:center;font-sie:1px'>-----------------------------------------------</td>
</tr>
</table>
<br>
<table width="100%">
<tr>
<th style='text-align:center;font-sie:1px'>Thank you Visit Again</th>
</tr>
</table>
<script>
window.close();
</script>
</html>
ボタンを1回クリックするだけで、phpとjavascriptを使用して新しいタブウィンドウを印刷して閉じます
これは、HTMLをポップアップに挿入するのに最適でした<body onload="window.print()"...
。上記はIE、Chrome、FF(Macの場合)では機能しますが、WindowsではFFは機能しません。
https://stackoverflow.com/a/11782214/1322092
var html = '<html><head><title></title>'+
'<link rel="stylesheet" href="css/mycss.css" type="text/css" />'+
'</head><body onload="window.focus(); window.print(); window.close()">'+
data+
'</body></html>';
これが私がすることです...
クエリパラメータに基づいてウィンドウを印刷して閉じることができるようにします。
jQueryが必要です。_Layoutまたはマスターページで実行して、すべてのページを操作できます。
アイデアは、ページに印刷して閉じるように指示するパラメータをURLに渡すことです。パラメータが設定されている場合、jQueryの「ready」イベントがウィンドウを印刷し、ページが完全に読み込まれると(印刷後)「onload」ウィンドウを閉じると呼ばれます。この一見余分な手順はすべて、ウィンドウが印刷されるのを待ってから閉じることです。
html本体で、printAndCloseOnLoad()を呼び出すonloadイベントを追加します。この例ではcshtmを使用していますが、javascriptを使用してparamを取得することもできます。
<body onload="sccPrintAndCloseOnLoad('@Request.QueryString["PrintAndClose"]');">
javascriptに関数を追加します。
function printAndCloseOnLoad(printAndClose) {
if (printAndClose) {
// close self without prompting
window.open('', '_self', ''); window.close();
}
}
そしてjQueryの準備ができたイベント。
$(document).ready(function () {
if (window.location.search.indexOf("PrintAndClose=") > 0)
print();
});
これで、URLを開くときに、クエリ文字列param“ PrintAndClose = true”を追加するだけで、印刷されて閉じられます。
私にとって、私の最終的な解決策は、いくつかの答えを組み合わせることでした。
var newWindow = window.open();
newWindow.document.open();
newWindow.document.write('<html><link rel="stylesheet" href="css/normalize-3.0.2.css" type="text/css" />'
+ '<link rel="stylesheet" href="css/default.css" type="text/css" />'
+ '<link rel="stylesheet" media="print" href="css/print.css" type="text/css" />');
newWindow.document.write('<body onload="window.print();" onfocus="window.setTimeout(function() { window.close(); }, 100);">');
newWindow.document.write(document.getElementById(<ID>).innerHTML);
newWindow.document.write('</body></html>');
newWindow.document.close();
newWindow.focus();
これは私のために働いたものです(2018/02)。印刷物がまだ画面に表示されていないため、別のリクエストが必要でした。上記の優れた回答のいくつかに基づいて、私はすべてに感謝します、私は気づきました:
w.onload
の前に設定しないでくださいw.document.write(data)
。document.write()
場合は、処理が終了したときにフックが呼び出されます。w.document.close()
まだ必要です。それ以外の場合は何も起こりません。これをChrome64.0、IE11(11.248)、Edge 41.16299(edgeHTML 16.16299)、FF58.0.1でテストしました。彼らはポップアップについて文句を言うでしょう、しかしそれは印刷します。
function on_request_print() {
$.get('/some/page.html')
.done(function(data) {
console.log('data ready ' + data.length);
var w = window.open();
w.document.write(data);
w.onload = function() {
console.log('on.load fired')
w.focus();
w.print();
w.close();
}
console.log('written data')
//this seems to be the thing doing the trick
w.document.close();
console.log('document closed')
})
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<a onclick="on_request_print();">Print rapportage</a>
const printHtml = async (html) => {
const printable = window.open('', '_blank', 'fullscreen=no');
printable.document.open();
printable.document.write(`<html><body onload="window.print()">${html}</body></html>`);
await printable.print();
printable.close();
};
これが私のES2016ソリューションです。
これは私にとっては完全に@holgerで機能しますが、私はそれを変更して私に合っています。印刷またはキャンセルボタンを押すとすぐにウィンドウがポップアップして閉じます。
function printcontent()
{
var disp_setting="toolbar=yes,location=no,directories=yes,menubar=yes,";
disp_setting+="scrollbars=yes,width=300, height=350, left=50, top=25";
var content_vlue = document.getElementById("content").innerHTML;
var w = window.open("","", disp_setting);
w.document.write(content_vlue); //only part of the page to print, using jquery
w.document.close(); //this seems to be the thing doing the trick
w.focus();
w.print();
w.close();
}"
このようなものをブラウザ間で機能させるには、多くの苦痛があります。
私はもともと同じようなことをしたいと思っていました。印刷用にスタイル設定された新しいページを開き、JSを使用して印刷してから、もう一度閉じます。これは悪夢でした。
結局、私は単にクリックスルーして印刷可能なページに移動し、次に以下のJSを使用して印刷を開始し、完了したら目的の場所にリダイレクトすることを選択しました(この場合はPHPで変数を設定します)。
私はこれをOSXとWindowsのChromeとFirefox、およびIE11-8でテストしましたが、すべてで動作します(ただし、実際にプリンターをインストールしていない場合、IE8は少しフリーズします)。
ハッピーハンティング(印刷)。
<script type="text/javascript">
window.print(); //this triggers the print
setTimeout("closePrintView()", 3000); //delay required for IE to realise what's going on
window.onafterprint = closePrintView(); //this is the thing that makes it work i
function closePrintView() { //this function simply runs something you want it to do
document.location.href = "'.$referralurl.'"; //in this instance, I'm doing a re-direct
}
</script>
このJavaスクリプトを使用するだけです
function PrintDiv() {
var divContents = document.getElementById("ReportDiv").innerHTML;
var printWindow = window.open('', '', 'height=200,width=400');
printWindow.document.write('</head><body >');
printWindow.document.write(divContents);
printWindow.document.write('</body></html>');
printWindow.document.close();
printWindow.print();
printWindow.close();
}
送信またはキャンセルボタンをクリックするとウィンドウが閉じます
IE11では、onfocusイベントが2回呼び出されるため、ユーザーはウィンドウを閉じるように2回求められます。これは、わずかな変更で防ぐことができます。
<script type="text/javascript">
var isClosed = false;
window.print();
window.onfocus = function() {
if(isClosed) { // Work around IE11 calling window.close twice
return;
}
window.close();
isClosed = true;
}
</script>
これは、FF 36、Chrome 41、およびIE 11で機能しました。印刷をキャンセルした場合でも、右上の「X」で印刷ダイアログを閉じた場合でも。
var newWindow=window.open();
newWindow.document.open();
newWindow.document.write('<HTML><BODY>Hi!</BODY></HTML>'); //add your content
newWindow.document.close();
newWindow.print();
newWindow.onload = function(e){ newWindow.close(); }; //works in IE & FF but not chrome
//adding script to new document below makes it work in chrome
//but alone it sometimes failed in FF
//using both methods together works in all 3 browsers
var script = newWindow.document.createElement("script");
script.type = "text/javascript";
script.text = "window.close();";
newWindow.document.body.appendChild(script);
setTimeout(function () { window.print(); }, 500);
window.onfocus = function () { setTimeout(function () { window.close(); }, 500); }
それは私にとって完璧に機能します。それが役に立てば幸い