Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/479.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 链接以打印URL_Javascript_Html_Http_Printing_Http Headers - Fatal编程技术网

Javascript 链接以打印URL

Javascript 链接以打印URL,javascript,html,http,printing,http-headers,Javascript,Html,Http,Printing,Http Headers,我想打开一个页面,但它要求用户打印,而不是查看它 这与HTTP头的功能非常相似 当设置为附件时,它将询问用户保存文件的位置(而不是在浏览器中显示) 我有权为用户打开页面,以便: var doc=open(url)//为他们打开链接,而不是使用锚href doc.print()//要求他们打印,这是一个阻塞呼叫,直到用户完成 doc.close()//立即关闭窗口,这样用户就不会被打断 但我真的希望有一些服务器端的标志,我可以利用,有这样的事情吗 打开的页面不一定是HTML文档,因此使用wind

我想打开一个页面,但它要求用户打印,而不是查看它

这与HTTP头的功能非常相似
当设置为
附件
时,它将询问用户保存文件的位置(而不是在浏览器中显示)

我有权为用户打开页面,以便:

var doc=open(url)//为他们打开链接,而不是使用锚href
doc.print()//要求他们打印,这是一个阻塞呼叫,直到用户完成
doc.close()//立即关闭窗口,这样用户就不会被打断
但我真的希望有一些服务器端的标志,我可以利用,有这样的事情吗


打开的页面不一定是HTML文档,因此使用
window.print();window.close()在所有情况下都不起作用。

您已经混淆了服务器端和客户端语言的功能

服务器端语言(如PHP或ASP)在服务器上执行一些操作,比如在网上商店计算价格

contentdisposition:attachment
头在这方面有点奇怪,因为它控制的是客户机而不是服务器

客户端语言(本例中为JavaScript)执行用户浏览器上发生的操作


打印是客户端功能。您需要使用JavaScript。

我决定用JavaScript发布一个答案,因为它其实并不简单:(
我在问题中所写的内容无法跨浏览器使用(实际上是Firefox,这次不是IE!)

问题是,在Firefox中,实际上是非阻塞的,因此在我上面的示例中,新的
open()
'd窗口将在Firefox打印之前关闭

因此,您可以保持窗口打开,也可以尝试使用隐藏框架

function printUrl(url) {
    var frame = document.createElement('iframe');
    frame.setAttribute('src', url);

    //must be in the DOM to work in Firefox/IE
    document.body.appendChild(frame);

    //since it's in the DOM we need to hide it (lest we ruin our page layout)
    //can't use 'display:none' or 'visibility:hidden' because you can't get focus (needed for IE)
    //'z-index:-1' & 'opacity:0' wont help if there is no background in the elements above it and someone clicks through to it
    frame.style.position = 'absolute';
    frame.style.left = '-9999px';

    //when it is loaded, print it
    function printFrame() {
        //have to get focus in IE
        frame.contentWindow.focus();

        //print!
        frame.contentWindow.print();

        /* cant remove the element because print() is non-blocking in FF
         * (I.e. it would remove the frame before it was printed!)
        //cleanup
        //document.body.removeChild(frame);*/
    };

    //if it was cached, it may already be done (onload wont fire, IE8)
    if (frame.contentWindow && frame.contentDocument.readyState == 'complete')
        printFrame();
    else {
        if (frame.addEventListener)
            //W3C
            frame.addEventListener('load', printFrame, false);
        else
            //IE<9
            frame.attachEvent('onload', printFrame);
    }
}
函数printUrl(url){
var frame=document.createElement('iframe');
frame.setAttribute('src',url);
//必须在DOM中才能在Firefox/IE中工作
document.body.appendChild(框架);
//因为它在DOM中,我们需要隐藏它(以免破坏页面布局)
//无法使用“显示:无”或“可见性:隐藏”,因为无法获得焦点(IE所需)
//“z-index:-1”和“opacity:0”如果上面的元素中没有背景,并且有人点击了它,则不会有帮助
frame.style.position='绝对';
frame.style.left='-9999px';
//加载后,打印它
函数printFrame(){
//在IE中必须集中精力
frame.contentWindow.focus();
//打印!
frame.contentWindow.print();
/*无法删除元素,因为print()在FF中是非阻塞的
*(即,它将在打印前移除框架!)
//清理
//document.body.removeChild(框架)*/
};
//如果缓存了,则可能已经完成了(onload不会触发,IE8)
if(frame.contentWindow&&frame.contentDocument.readyState=='complete')
printFrame();
否则{
if(frame.addEventListener)
//W3C
frame.addEventListener('load',printFrame,false);
其他的
//IE7

请注意,与简单的
open()
(如果有效)一样,这不适用于跨站点。您无法访问不同域中的
窗口
页面方法,无论是在弹出窗口还是框架中。

HTTP的可能副本不是HTML,但您的回答似乎表明HTTP没有任何好处。所以,我想,这个问题是指打印它当前所在的页面。不链接到一个打印对话的文档。没错。我正在寻找更多这些奇怪的东西,如果它们exist@Hashbrown没有什么可打印的。