打印外部页面没有打开它 [英] Print external page without open it

查看:133
本文介绍了打印外部页面没有打开它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要打印页面,而不打开它,在所有主要的浏览器。 (Safari浏览器,IE,火狐,Chrome和Opera)

I want print a page without open it on all major browsers. (Safari, IE, firefox, Chrome and Opera)

我试过,但在Firefox(错误:权限被拒绝访问财产'打印'):不工作

I tried that but doesn't work on firefox (Error : Permission denied to access property 'print') :

<!DOCTYPE html>
    <html>
        <head>
            <title>Test</title>
            <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
            <link rel="alternate" media="print" href="print.php"> 
            <script type="text/javascript">
                function impression() {
                window.frames[0].focus();
                window.frames[0].print();
                }
            </script>
        </head>
        <body>


            <iframe height="0px" src="print.php" id="fileToPrint" style="visibility: hidden"></iframe>
            <a href="javascript:impression()">Imprimer</a>
        </body>
    </html>

这code适用于Chrome。

This code works on Chrome.

我想一件事一样,对所有的浏览器提,但我不知道怎么办。

I want one thing like that for all browsers to mention but I don't know how.

有另一种方式来做到这一点?

Is there another way to do that?

推荐答案

创建一个iframe,其隐藏,然后调用适当的打印功能。在将execCommand应适用于IE浏览器的所有版本。结果
提醒你:$ .browser不会为jQuery的较新版本的工作,应尽量避免。使用的检测功能,您的preferred方式。

Create an iframe, hide it, and then call the proper print functions. The execCommand should work for all versions of IE.
Mind you: $.browser won't work for newer versions of jQuery and should be avoided. Use your preferred way of detecting features.

var ifr = createIframe();
ifr.hide();

if ($.browser.msie) {
    ifr.contentWindow.document.execCommand('print', false, null);
} else {
    ifr.contentWindow.focus();
    ifr.contentWindow.print();
}

这是为IE,FF和Chrome开发。我不知道如何这会为Safari和Opera的工作,但它可能给你的一些想法。

This was developed for IE, FF and Chrome. I have no idea how well this will work for Safari and Opera, but it might give you some ideas.

编辑:作为adeneo正确地指出的那样,$ .browser是德precated,应该避免。我更新了我的发言。我会离开我的code不变,因为它仍然EX presses正确的意图。

as adeneo correctly pointed out, $.browser is deprecated and should be avoided. I updated my statement. I'll leave my code untouched, as it still expresses the correct intent.

这篇关于打印外部页面没有打开它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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