[英]How can I pop-up a print dialog box using Javascript?
提示:本站为国内最大中英文翻译问答网站,提供中英文对照查看,鼠标放在中文字句上可显示英文原文。
我有一个带有“打印”链接的页面,该链接将用户带到对打印机友好的页面。 客户希望在用户到达打印友好页面时自动出现一个打印对话框。 如何使用 javascript 做到这一点?
window.print();
除非您的意思是自定义外观的弹出窗口。
你可以做
<body onload="window.print()">
...
</body>
我喜欢这个,这样你就可以添加任何你想要的字段并以这种方式打印。
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>
如果您只有一个没有点击事件处理程序的链接:
<a href="javascript:window.print();">Print Page</a>
我知道答案已经提供了。 但我只是想详细说明在 Blazor 应用程序(剃须刀)中执行此操作...
您将需要注入 IJSRuntime,以执行 JSInterop(从 C# 运行 javascript 函数)
在您的剃刀页面:
@inject IJSRuntime JSRuntime
注入后,创建一个带有调用 C# 方法的单击事件的按钮:
<MatFAB Icon="@MatIconNames.Print" OnClick="@(async () => await print())"></MatFAB>
(或者如果你不使用 MatBlazor 更简单的东西)
<button @onclick="@(async () => await print())">PRINT</button>
对于 C# 方法:
public async Task print()
{
await JSRuntime.InvokeVoidAsync("printDocument");
}
现在在你的 index.html 中:
<script>
function printDocument() {
window.print();
}
</script>
需要注意的是,onclick 事件是异步的原因是因为 IJSRuntime 等待它的调用,例如 InvokeVoidAsync
PS:例如,如果您想在asp net core中发送消息框:
await JSRuntime.InvokeAsync<string>("alert", "Hello user, this is the message box");
有一个确认消息框:
bool question = await JSRuntime.InvokeAsync<bool>("confirm", "Are you sure you want to do this?");
if(question == true)
{
//user clicked yes
}
else
{
//user clicked no
}
希望这可以帮助 :)
您可以将其绑定到按钮或页面加载。
window.print();
<script>
const _print = () => {
window.print();
}
</script>
或者
<body onload="window.print();"></body>
请参阅此处的文档: https : //developer.mozilla.org/en-US/docs/Web/API/Window/print
我知道这是一个老问题,但是在解决了一个类似的问题之后,我想出了一种方法来打开打印屏幕,而不必打开新标签,也不必启用弹出窗口。
希望这对其他人有帮助。
/*
Example:
<a href="//example.com" class="print-url">Print</a>
*/
//LISTEN FOR PRINT URL ITEMS TO BE CLICKED
$(document).off('click.PrintUrl').on('click.PrintUrl', '.print-url', function(e){
//PREVENT OTHER CLICK EVENTS FROM PROPAGATING
e.preventDefault();
//TRY TO ASK THE URL TO TRIGGER A PRINT DIALOGUE BOX
printUrl($(this).attr('href'));
});
//TRIGGER A PRINT DIALOGE BOX FROM A URL
function printUrl(url) {
//CREATE A HIDDEN IFRAME AND APPEND IT TO THE BODY THEN WAIT FOR IT TO LOAD
$('<iframe src="'+url+'"></iframe>').hide().appendTo('body').on('load', function(){
var oldTitle = $(document).attr('title'); //GET THE ORIGINAL DOCUMENT TITLE
var that = $(this); //STORE THIS IFRAME AS A VARIABLE
var title = $(that).contents().find('title').text(); //GET THE IFRAME TITLE
$(that).focus(); //CALL THE IFRAME INTO FOCUS (FOR OLDER BROWSERS)
//SET THE DOCUMENT TITLE FROM THE IFRAME (THIS NAMES THE DOWNLOADED FILE)
if(title && title.length) $(document).attr('title', title);
//TRIGGER THE IFRAME TO CALL THE PRINT
$(that)[0].contentWindow.print();
//LISTEN FOR THE PRINT DIALOGUE BOX TO CLOSE
$(window).off('focus.PrintUrl').on('focus.PrintUrl', function(e){
e.stopPropagation(); //PREVENT OTHER WINDOW FOCUS EVENTS FROM RUNNING
$(that).remove(); //GET RID OF THE IFRAME
if(title && title.length) $(document).attr('title', oldTitle); //RESET THE PAGE TITLE
$(window).off('focus.PrintUrl'); //STOP LISTENING FOR WINDOW FOCUS
});
});
};
如果问题:
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;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.