window.close();在window.print()之后;javascript

window.close();在window.print()之后;javascript,javascript,Javascript,我需要一个js代码,它将关闭窗口后出现的弹出式打印窗口。这是我的密码: <a href="javascript:;" onclick="print()">Print</a> function print() { win = window.open(); win.document.write('<html><head>blablabla.......'); win.window.print(); win.win

我需要一个js代码,它将关闭窗口后出现的弹出式打印窗口。这是我的密码:

<a href="javascript:;" onclick="print()">Print</a>

function print()
{   
    win = window.open();
    win.document.write('<html><head>blablabla.......');
    win.window.print();
    win.window.close();
}
但它不起作用,因为窗口在弹出之前就关闭了

<a href="javascript:;" onclick="print()">Print</a>

    function print()
    {   
        win = window.open();
        win.document.write('<html><head>blablabla.......');

        var document_focus = false; // var we use to monitor document focused status.
        // Now our event handlers.
        $(document).ready(function() { win.window.print();document_focus = true; });
        setInterval(function() { if (document_focus === true) { win.window.close(); }  }, 300);
    }
谢谢你的帮助


谢谢。

这样,打印后窗口将关闭,效果完美

function printme() {         
     var newWindow = window.open();
     newWindow.document.open("text/html");
     newWindow.document.write('<html><head>blablabla.......');
     newWindow.document.close();

     newWindow.print();
     newWindow.onfocus = function () {
        newWindow.close(); // Close the window
     }
};

这样,打印后窗口将关闭,效果完美

function printme() {         
     var newWindow = window.open();
     newWindow.document.open("text/html");
     newWindow.document.write('<html><head>blablabla.......');
     newWindow.document.close();

     newWindow.print();
     newWindow.onfocus = function () {
        newWindow.close(); // Close the window
     }
};

这个代码对我有用:

print = function() {
    var printContents = document.getElementById('div').innerHTML,
      params = [
        'height=' + screen.height,
        'width=' + screen.width,
        'fullscreen=yes'].join(','),

    popup = window.open("", 'popUpWindow', params);
    popup.document.write('<html><head><link rel="stylesheet" type="text/css" href="/style.css" /></head><body class="print" onload="window.print()" onmousemove="setTimeout(function () {window.close()},1000);" >'
      + printContents
      + '</body></html>');
    popup.onfocus = function () {
      setTimeout(function () {
        popup.focus();
        popup.document.close();
      }, 200);
    };
};

这个代码对我有用:

print = function() {
    var printContents = document.getElementById('div').innerHTML,
      params = [
        'height=' + screen.height,
        'width=' + screen.width,
        'fullscreen=yes'].join(','),

    popup = window.open("", 'popUpWindow', params);
    popup.document.write('<html><head><link rel="stylesheet" type="text/css" href="/style.css" /></head><body class="print" onload="window.print()" onmousemove="setTimeout(function () {window.close()},1000);" >'
      + printContents
      + '</body></html>');
    popup.onfocus = function () {
      setTimeout(function () {
        popup.focus();
        popup.document.close();
      }, 200);
    };
};
JQuery中的这个片段还打印html中呈现的动态图像

  <button id="printreceipt">
  Print
  </button>
  <div id="receipt">
  Colombo was great..
  </div>

$(function(){
$('body').on('click','button#printreceipt',function(e){
e.preventDefault();
e.stopPropagation();

var printWindow = window.open('');
var divContents = $("#receipt").html() +
                    "<script>" +
                    "window.onload = function() {" +
                    "     window.print();" +
                    "};" +
                   "setInterval(function() {   {   clearInterval();window.close(); }  }, 300);" +
                    "<" + "/script>";
 printWindow.document.write(divContents);
 printWindow.document.close();
 });
});
JQuery中的这个片段还打印html中呈现的动态图像

  <button id="printreceipt">
  Print
  </button>
  <div id="receipt">
  Colombo was great..
  </div>

$(function(){
$('body').on('click','button#printreceipt',function(e){
e.preventDefault();
e.stopPropagation();

var printWindow = window.open('');
var divContents = $("#receipt").html() +
                    "<script>" +
                    "window.onload = function() {" +
                    "     window.print();" +
                    "};" +
                   "setInterval(function() {   {   clearInterval();window.close(); }  }, 300);" +
                    "<" + "/script>";
 printWindow.document.write(divContents);
 printWindow.document.close();
 });
});

这就是我在Chrome上工作的原因:

var html = "<html>...</html>";
var win = window.open();
win.document.write(html);
win.document.close();
setTimeout(function () {
   win.focus();
   win.print();
   win.close();
}, 100);

关键的一点是进行焦点和打印异步。没有这一点,我在打印预览中看到一个空白页。

这就是我在Chrome上工作的原因:

var html = "<html>...</html>";
var win = window.open();
win.document.write(html);
win.document.close();
setTimeout(function () {
   win.focus();
   win.print();
   win.close();
}, 100);

关键的一点是进行焦点和打印异步。如果没有,我会在打印预览中看到一个空白页。

删除win.window.close@DontVoteMeDown关闭窗口..这到底是怎么回事..你应该在打印之前保持窗口的活动性..不要辱骂和冒犯..好吗?我想你可以找到你想要的东西删除win.window.close@不要关闭窗户。这到底是怎么回事。你们应该保持窗户的活力,直到你们打印出来。不要辱骂和冒犯。好吗?我想你们能找到你们想要的