2017-05-19 34 views
0

它在本地运行的很好,但是当我将它上传到WordPress网站时,新的打印窗口打开并立即闪烁开/关屏幕。只用Chrome进行测试,就像我说过的,直到我将其上传到服务器,我才有问题?打印窗口在本地工作,但不在网站上w/WordPress

的Javascript:

function printDiv(divName) { 

"use strict"; 
var divToPrint = document.getElementById(divName); 

var WindowObject = window.open('', 'PrintWindow', 'width=750,height=650,top=50,left=50,toolbars=no,scrollbars=yes,status=no,resizable=yes'); 
WindowObject.document.writeln('<!DOCTYPE html>'); 
WindowObject.document.writeln('<html><head><title>Your Safari Print Results</title>'); 
WindowObject.document.writeln('<link rel="stylesheet" id="fonts-googleapis-css" href="http://fonts.googleapis.com/css?family=Lato%3A400%2C100%2C300%2C700%2C900&#038;ver=4.6" type="text/css" media="all">'); 
WindowObject.document.writeln('<link rel="stylesheet" type="text/css" href="NewWinPrintScreen_hmap.css">'); 
WindowObject.document.writeln('</head><body onload="window.print()"><center><img src="logo.png" width="219" height="78"><br><div class="line"></div></center><br><br><span class="pg_title">Canine Wellness HealthMap</span>'); 

WindowObject.document.writeln(divToPrint.innerHTML); 

WindowObject.document.writeln('</body></html>'); 

WindowObject.document.close(); 
setTimeout(function() { WindowObject.close(); }, 500); 

}

编辑了以上(打印窗口现在住了,但是没有内容在Chrome中显示,在FF作品)

function printDiv(divName) { 

“使用严格” ;

var WindowObject = window.open('', 'PrintWindow', 'width=750,height=650,top=50,left=50,toolbars=no,scrollbars=yes,status=no,resizable=yes'); 
var divToPrint = document.getElementById(divName); 
WindowObject.document.writeln('<!DOCTYPE html><html><head><title>Your Safari Print Results</title><link rel="stylesheet" type="text/css" href="css/NewWinPrintScreen_hmap.css"></head><body><center><img src="images/logo.png" width="219" height="78"><br><div class="line"></div></center><br><br><span class="pg_title">Canine Wellness HealthMap</span>'); 
WindowObject.document.writeln(divToPrint.innerHTML); 

WindowObject.document.writeln('</body></html>'); 

/* Delaying the print event */ 
setInterval(function() {  }, 100); 
    WindowObject.focus(); 
    WindowObject.print(); 
    WindowObject.close(); 

}

+0

你有没有[检查你的控制台?](http://stackoverflow.com/documentation/javascript/185/getting-started-with-javascript/714/using-console-log) –

+0

不给我任何错误。我玩了脚本,并得到它的打印窗口保持不变,但内容不在预览中,它打印空白。尽管在Firefox中工作。 – flipflopmedia

回答

0

得到它在Chrome中正常工作。打开打印窗口,预览并在之后关闭。在这段代码和确保jQuery被加载之间,它现在在Chrome中完全正常运行!

此脚本允许您在同一页面上打印多个/单独的DIV。将打印按钮指定给DIV,这就是打印的DIV!

function printDiv(divName) { // jshint ignore:line 

"use strict"; 
var divToPrint = document.getElementById(divName); 
var mywindow = window.open('', 'PrintWindow', 'width=750,height=650,top=50,left=50,toolbars=no,scrollbars=yes,status=no,resizable=yes'); 
mywindow.document.write('<html><head><title>Your Safari Print Results</title><link rel="stylesheet" type="text/css" href="css/NewWinPrintScreen_hmap.css"></head>'); 
mywindow.document.write('<body><center><img src="images/logo.png" width="219" height="78"><br><div class="line"></div></center><br><br><span class="pg_title">Canine Wellness HealthMap</span>'); 

mywindow.document.write(divToPrint.innerHTML); 

mywindow.document.write('</body></html>'); 

setTimeout(function() { // wait until all resources loaded 
    mywindow.document.close(); // necessary for IE >= 10 
    mywindow.focus(); // necessary for IE >= 10 
    mywindow.print(); // change window to winPrint 
    mywindow.close(); // change window to winPrint 
}, 3000); 

return true; }

,并且打印按钮代码(命名的DIV任何你喜欢):

<button onclick="Javascript:printDiv('Print-Needs-LifeStyle-Life-Stage')">Print</button>

FYI:更改3000到500,因为它太长了打印预览窗口来在新窗口打开后。

相关问题