Как я могу открыть диалоговое окно печати с помощью Javascript?
У меня есть страница со ссылкой "печать", которая выводит пользователя на страницу, удобную для печати. Клиент хочет, чтобы диалоговое окно печати появлялось автоматически, когда пользователь приходит на страницу, удобную для печати. Как я могу сделать это с помощью JavaScript?
6 ответов:
Мне нравится это, так что вы можете добавить любые поля, которые вы хотите, и распечатать его таким образом.
function printPage() { var w = window.open(); var headers = $("#headers").html(); var field= $("#field1").html(); var field2= $("#field2").html(); var html = "<!DOCTYPE HTML>"; html += '<html lang="en-us">'; html += '<head><style></style></head>'; html += "<body>"; //check to see if they are null so "undefined" doesnt print on the page. <br>s optional, just to give space if(headers != null) html += headers + "<br/><br/>"; if(field != null) html += field + "<br/><br/>"; if(field2 != null) html += field2 + "<br/><br/>"; html += "</body>"; w.document.write(html); w.window.print(); w.document.close(); };
Я делаю это, чтобы убедиться, что они не забывают печатать пейзаж, который необходим для многих страниц на многих принтерах.
<a href="javascript:alert('Please be sure to set your printer to Landscape.');window.print();">Print Me...</a>или
<body onload="alert('Please be sure to set your printer to Landscape.');window.print();"> etc. </body>
Если проблема:
mywindow.print();альтенативе через:
'<scr'+'ipt>print()</scr'+'ipt>'полный:
$('.print-ticket').click(function(){ var body = $('body').html(); var ticket_area = '<aside class="widget tickets">' + $('.widget.tickets').html() + '</aside>'; $('body').html(ticket_area); var print_html = '<html lang="tr">' + $('html').html() + '<scr'+'ipt>print()</scr'+'ipt>' + '</html>'; $('body').html(body); var mywindow = window.open('', 'my div', 'height=600,width=800'); mywindow.document.write(print_html); mywindow.document.close(); // necessary for IE >= 10'</html>' mywindow.focus(); // necessary for IE >= 10 //mywindow.print(); mywindow.close(); return true; });
Comments