Skip to content Skip to sidebar Skip to footer

Printing The Contents Of A Div Tag Without A POP-Up Window In Javascript

I'm struggling to print the contents of a div tag without a pop up window My code looks like this at the moment var DocumentContainer = document.getElementById('print'); v

Solution 1:

There is a more efficient way - without jQuery or any other library

The idea is to replace the window with a hidden iframe, and print the contents of the iframe.

here is the code for that:

    function ClickHereToPrint(){
      try{
        var oIframe = document.getElementById('ifrmPrint');
        var oContent = document.getElementById('divToPrint').innerHTML;
        var oDoc = (oIframe.contentWindow || oIframe.contentDocument);
        if (oDoc.document) oDoc = oDoc.document;
        oDoc.write('<head><title>title</title>');
        oDoc.write('</head><body onload="this.focus(); this.print();">');
        oDoc.write(oContent + '</body>');
        oDoc.close();
      } catch(e){
        self.print();
      }
    }

This considers that you have an iframe in your page. if you don't, just create one using: document.createElement('iframe')


Solution 2:

Try this function-

function PrintDiv(divid,title) {
    var contents = document.getElementById(divid).innerHTML;
    var frame1 = document.createElement('iframe');
    frame1.name = "frame1";
    frame1.style.position = "absolute";
    frame1.style.top = "-1000000px";
    document.body.appendChild(frame1);
    var frameDoc = frame1.contentWindow ? frame1.contentWindow : frame1.contentDocument.document ? frame1.contentDocument.document : frame1.contentDocument;
    frameDoc.document.open();
     frameDoc.document.write(`<html><head><title>${title}</title>`);
    frameDoc.document.write('</head><body>');
    frameDoc.document.write(contents);
    frameDoc.document.write('</body></html>');
    frameDoc.document.close();
    setTimeout(function () {
        window.frames["frame1"].focus();
        window.frames["frame1"].print();
        document.body.removeChild(frame1);
    }, 500);
    return false;
}

Fiddle Link

https://jsfiddle.net/rtg37u94/


Solution 3:


Solution 4:

What do you mean by "this"? What "this" is is code to open a window and write into it. If you want to do "this" by not using a popup window, then there's nothing left in the question.

Are you trying to write the contents of the <div> into the current document? Then simply do so!

var DocumentContainer = document.getElementById('print');
var WindowObject = window.open('', 'Completed Registration Form', <args>);
document.writeln(DocumentContainer.innerHTML);

Otherwise you need to be far more specific about what you wish to do.


Edit Oh, that kind of printing! Wow, people still do that?

$('#print').print();

in jQuery, using the technique that behowdle89 linked to.

Y'know, I imagine your users will wonder why they suddenly have a print dialog on their screen, and/or why their printer is suddenly whirring without notice. I suggest sending users to a PDF instead, which they can print on their own if they like, or save onto their hard disk if they have a bit more sense.


Solution 5:

all of the answers listed here use document.write which is bad practice, even to a temporary iframe.

try: https://github.com/jasonday/printThis

It lets you pull in the page styles, as well as allowing for an additional stylesheet all without document.write.


Post a Comment for "Printing The Contents Of A Div Tag Without A POP-Up Window In Javascript"