Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.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 打印外部页面而不打开它_Javascript_Php_Browser_Printing - Fatal编程技术网

Javascript 打印外部页面而不打开它

Javascript 打印外部页面而不打开它,javascript,php,browser,printing,Javascript,Php,Browser,Printing,我想打印一个页面,但不在所有主要浏览器上打开它。(Safari、IE、firefox、Chrome和Opera) 我试过了,但在firefox上不起作用(错误:访问“打印”属性的权限被拒绝): 试验 函数印象(){ window.frames[0].focus(); window.frames[0].print(); } 这段代码适用于Chrome 我希望所有浏览器都能提到这样一件事,但我不知道怎么做 还有其他方法吗?您可以尝试这段代码,但它是Javascript <script la

我想打印一个页面,但不在所有主要浏览器上打开它。(Safari、IE、firefox、Chrome和Opera)

我试过了,但在firefox上不起作用(错误:访问“打印”属性的权限被拒绝):


试验
函数印象(){
window.frames[0].focus();
window.frames[0].print();
}
这段代码适用于Chrome

我希望所有浏览器都能提到这样一件事,但我不知道怎么做


还有其他方法吗?

您可以尝试这段代码,但它是Javascript

<script language="JavaScript">
var gAutoPrint = true; // Tells whether to automatically call the print function

function printSpecial()
{ 
if (document.getElementById != null)
{
var html = '<HTML>\n<HEAD>\n';

if (document.getElementsByTagName != null)
{
var headTags = document.getElementsByTagName("head");
if (headTags.length > 0)
html += headTags[0].innerHTML;
}

html += '\n</HE>\n<BODY>\n';

var printReadyElem = document.getElementById("printReady");

if (printReadyElem != null)
{
html += printReadyElem.innerHTML;
}
else
{
alert("Could not find the printReady function");
return;
}

html += '\n</BO>\n</HT>';

var printWin = window.open("","printSpecial");
printWin.document.open();
printWin.document.write(html);
printWin.document.close();
if (gAutoPrint)
printWin.print();
}
else
{
alert("The print ready feature is only available if you are using an browser. Please update your browswer.");
}
}

</script>

var gAutoPrint=true;//指示是否自动调用打印函数
函数printSpecial()
{ 
if(document.getElementById!=null)
{
var html='\n\n';
if(document.getElementsByTagName!=null)
{
var headTags=document.getElementsByTagName(“head”);
如果(headTags.length>0)
html+=头标签[0]。innerHTML;
}
html+='\n\n';
var printReadyElem=document.getElementById(“printReady”);
if(printReadyElem!=null)
{
html+=printReadyElem.innerHTML;
}
其他的
{
警报(“找不到printReady函数”);
返回;
}
html+='\n\n';
var printWin=window.open(“,“printSpecial”);
printWin.document.open();
printWin.document.write(html);
printWin.document.close();
如果(gAutoPrint)
printWin.print();
}
其他的
{
警报(“打印就绪功能仅在您使用浏览器时可用。请更新您的浏览器。”);
}
}

创建一个iframe,隐藏它,然后调用适当的打印函数。execCommand应适用于IE的所有版本。
请注意:$.browser不适用于较新版本的jQuery,应该避免使用。使用您喜欢的检测功能的方法

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开发的。我不知道这对狩猎和歌剧会有多好,但它可能会给你一些想法


编辑:正如adeneo正确指出的,$.browser已被弃用,应该避免使用。我更新了我的声明。我将保持代码不变,因为它仍然表达了正确的意图。

这可以帮助您-->对于这一点,没有跨浏览器的解决方案,在某些浏览器中,它将与iFrame一起工作,在另一些浏览器中,您可能需要一个新窗口,如果我没有记错,上述五种浏览器中的任何一种都不起作用。请尝试替换
window.frames[0]
使用
document.getElementById('fileToPrint').contentWindow
并查看这在firefox中是否有效。为什么不使用框架来实现这一点?例如jQuery@Ma'moonAl-Akash-jQuery如何帮助打印?它不是“不确定的”
$。浏览器已被弃用并从jQuery中删除
var ifr = createIframe();
ifr.hide();

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