Print function not working in chrome state

I have a print () function on my website that is used to print a specific part of the website. It works great on Firefoxand even on Internet explorer, but doesn't work on chrome. It opens a dialog box and gets the number of pages, but cannot get the content (in chrome).

my code is below:

<a href="#" onclick="PrintElem('#press_releas11')"><img src="images/print_icon.png" width="35" height="23" /></a>

<div class="blog_post" id="press_releas11">
    <div class="post_title"><h3>title here</h3></div>
    <div class="post_article">content here</div>
</div>

script:

<script type="text/javascript">

function PrintElem(elem)
{
    Popup($(elem).html());
}

function Popup(data) 
{
    var mywindow = window.open('', 'my div', 'height=400,width=600');
    mywindow.document.write('<html><head><title>PressReleases</title>');
    mywindow.document.write('<link rel="stylesheet" href="css/main.css" type="text/css" />');
    mywindow.document.write('</head><body >');
    mywindow.document.write(data);
    mywindow.document.write('data');
    mywindow.document.write('</body></html>'); 

    mywindow.document.close(); // necessary for IE >= 10
    mywindow.focus(); // necessary for IE >= 10

    mywindow.print();
    mywindow.close();

    return true;
}

</script>
+4
source share
4 answers

Chrome doesn't seem to like it document.write(). This helped to add setTimeoutfrom 1000 ms after writing to the document and then call mywindow.print(), but since it is not very practical and you already seem to be using jQuery, here is a working solution:

https://jsfiddle.net/2n88pxee/

Chrome Firefox, .

:

function PrintElem(elem)
{
    Popup($(elem).html());
}

function Popup(data) 
{
    var mywindow = window.open('', 'my div', 'height=400,width=600');

    mywindow.document.head.innerHTML = '<title>PressReleases</title><link rel="stylesheet" href="css/main.css" type="text/css" />'; 
    mywindow.document.body.innerHTML = '<body>' + data + '</body>'; 

    mywindow.document.close();
    mywindow.focus(); // necessary for IE >= 10
    mywindow.print();
    mywindow.close();

    return true;
}
+6

, , "", , .

, , readystate complete

function Popup(data) 
{
    var mywindow = window.open('', 'mydiv', 'height=400,width=600');
    mywindow.document.open();
    mywindow.document.onreadystatechange=function(){
     if(this.readyState==='complete'){
      this.onreadystatechange=function(){};
      mywindow.focus();
      mywindow.print();
      mywindow.close();
     }
    }
    mywindow.document.write('<html><head><title>PressReleases</title>');
    mywindow.document.write('<link rel="stylesheet" href="css/main.css" type="text/css" />');
    mywindow.document.write('</head><body >');
    mywindow.document.write(data);
    mywindow.document.write('data');
    mywindow.document.write('</body></html>'); 

    mywindow.document.close(); // necessary for IE >= 10


    return true;
}

http://jsfiddle.net/doktormolle/q2zv4s54/

+4

document.write document.close. setInterval/clearInterval, , . Chrome Mac:

function Popup(data) 
{
    var mywindow = window.open('', 'my div', 'height=400,width=600');
    mywindow.document.write('<html><head><title>PressReleases</title>');
    mywindow.document.write('<link rel="stylesheet" href="css/main.css" type="text/css" />');
    mywindow.document.write('</head><body >');
    mywindow.document.write(data);
    mywindow.document.write('data');
    mywindow.document.write('</body></html>');
    mywindow.document.close(); // necessary for IE >= 10

    myDelay = setInterval(checkReadyState, 10);

    function checkReadyState() {
        if (mywindow.document.readyState == "complete") {
            clearInterval(myDelay);
            mywindow.focus(); // necessary for IE >= 10

            mywindow.print();
            mywindow.close();
        }
    }

    return true;
}
+2

, - , SetTimeOut() :

    var mywindow = window.open('', 'PRINT', 'height=5100,width=1086');
    mywindow.document.write('<html><head><title>' + document.title + '</title>');
    mywindow.document.write('<link rel="stylesheet" href="App_Themes/Basic/Share/Workflow.css" type="text/css" />');
    mywindow.document.write('</head><body >');
    mywindow.document.write("SomeData");
    mywindow.document.close(); // necessary for IE >= 10
    mywindow.focus(); // necessary for IE >= 10*/

    setTimeout(function(){ mywindow.print(); mywindow.close();}, 2000);
Hide result
+2

Source: https://habr.com/ru/post/1584293/


All Articles