如何使用JavaScript打开带有打印对话框的文件 [英] How to open a file with print dialogue box using JavaScript

查看:41
本文介绍了如何使用JavaScript打开带有打印对话框的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用JavaScript打开一个word文档,并为该打开的文档窗口打开打印对话框.

I want to open one word document using JavaScript as well as open print dialogue box for that opened document window.

这是我的代码.

window.open('http://www.tizaq.com');

window.print();

它可以工作,但是将为当前窗口(而不是新打开的窗口)打开打印对话框.我该怎么办?

It works, but the print dialogue gets opened for the current window, not the newly opened window. How do I do it?

推荐答案

在新窗口而不是旧窗口上调用 print :

Call print on the new window rather than the old:

var wnd = window.open('http://stackoverflow.com');
wnd.print();

不过,我不喜欢您的赔率,因为它没有受到浏览器安全性的影响.:-)外部"窗口对象很可能不支持 print (( window 对象)有两种类型,其他窗口可以访问的外部"类型,以及引用自己的内部"类型,它具有更多权限,等等.)至少,您可能必须等待load事件,但我总的来说最好这会很棘手.

I don't like your odds, though, of it not falling afoul of browser security. :-) The "external" window object may well not support print (window objects come in two types, the "external" type that other windows have access to, and the "internal" type that references itself, which has more permissions, etc.) At the least, you'll probably have to wait for the load event, but I best in general it's going to be tricky.

对于具有相同来源的文档,它似乎可以使用,因此

It seems to work for documents with the same origin, so the Same Origin Policy is a factor. That example crashes in IE6 (literally crashes the browser), but works for me in IE7 on Windows, and Chrome and Firefox 3.6 on Linux (and not in Opera 11 on Linux). Probably wouldn't hurt to put a delay / yield in there, e.g.:

var wnd = window.open(your_path_here);
setTimeout(function() {
    wnd.print();
}, 0);

您在问题中说了"word文档",但您的示例看起来像一个网站.我不知道如果您通过将Microsoft Word文档加载到浏览器窗口中来打开它,是否可行.

You said "word document" in your question, but your example looks like a website. I have no idea whether this would work if you were opening a Microsoft Word document by loading it into a browser window.

这篇关于如何使用JavaScript打开带有打印对话框的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆