2

jQuery

 $(function(){
    $("#btnPrint").click(function(){
      var mydiv = document.getElementById("printDiv");
    printDiv(mydiv);
    });

    });

    function printDiv(divP) {

                window.frames["print_frame"].document.body.innerHTML = $(divP).html();
                window.frames["print_frame"].window.focus();
                window.frames["print_frame"].window.print();

            }

HTML

 <iframe name="print_frame" width="0" height="0" frameborder="0" src="about:blank">
        </iframe>

<div id="printDiv">
//some content here
</div>

<input type="button" id="btnPrint" value="Print"/>

The above code is working in IE and Mozilla but not working in Safari,chome and Opera? Please help..

Billy
  • 1,196
  • 5
  • 17
  • 33
  • It works fine for me (chrome 31.0.1650.63) maybe you have some other JS error on page? – Arthur Halma Dec 12 '13 at 09:56
  • "Not working" means what exactly? What does it do and what is it supposed to do? Are there errors in the [JavaScript error console](http://www.netmagazine.com/tutorials/javascript-debugging-beginners)? – JJJ Dec 12 '13 at 09:58
  • No errors. it is printing white page. – Billy Dec 12 '13 at 09:59
  • Check your chrome version. Its working fine for mi. Chrome version 31.0.1650.63 – Suhas Gosavi Dec 12 '13 at 10:02
  • works for me with latest chrome http://jsfiddle.net/Nu5SX/ – roo2 Dec 12 '13 at 10:05
  • 1
    heres a better way to print a single div http://stackoverflow.com/questions/468881/print-div-id-printarea-div-only – roo2 Dec 12 '13 at 10:06

1 Answers1

5

It looks like the purpose of this demo is to print only a single div, is that correct?

If so, it is easiest to use media queries for a cross browser solution

http://jsfiddle.net/Nu5SX/2/

@media print {
    body * {
       visibility: hidden;
    }
    #printDiv, #printDiv * {
       visibility: visible;
    }
    #printDiv {
       position: absolute;
       left: 0;
       top: 0;
    }
}

simple javascript

    window.print();

EDIT: sorry for talking like clippy :)

Community
  • 1
  • 1
roo2
  • 5,781
  • 2
  • 29
  • 45
  • Thanks but I want to hide the printDiv by default. just checkout: http://jsfiddle.net/Nu5SX/3/ – Billy Dec 12 '13 at 11:12
  • 1
    I used a modified version to print a specific part of page online. Thanks for your inspiration! – Tim Oct 22 '14 at 09:48