0

Upon clicking the Print button I get a Print Dialog.

Actual outcome: When I click Cancel on Print Dialog, it shows the print preview in the webpage.

Expected outcome: What I want is to show my actual webpage I was on prior to the Print Dialog bog.

Code for Printing:

function printMap(){ 
    var printMapOnly = document.getElementById('regions_div').innerHTML;
    document.body.innerHTML = printMapOnly;
    window.print();
    }

Print button:

<button class="btn btn-print" onclick="printMap('regions_div')">Print</button>
Zane
  • 33
  • 1
  • 8
  • You're erasing the original page content when you set that (I'm guessing partial) page element to `document.body.innerHTML`. You might try a [CSS print stylesheet](https://www.smashingmagazine.com/2011/11/how-to-set-up-a-print-style-sheet/) and more or less hide what you don't want to print. – Jared Farrish Jun 17 '17 at 02:44
  • Instead of going through all that process, can't I set `document.body.innerhtml` to the state it was in before? – Zane Jun 17 '17 at 03:04

1 Answers1

1

Based on this I set my webpage's content back to its original content

Updated:

function printMap(){ 
    var originalContent = document.body.innerHTML
    var printMapOnly = document.getElementById('regions_div').innerHTML;
    document.body.innerHTML = printMapOnly;
    window.print();

    document.body.innerHTML = originalContent;
    }

Hope it helps someone else.

Zane
  • 33
  • 1
  • 8
  • The problem with that technique is that it doesn't take into account the HTML having any behaviors, e.g., event handlers and such. This will generally work with fairly straight-forward mainly layout sites, but be forewarned. – Jared Farrish Jun 17 '17 at 17:48
  • I know the drawback of it however, the website I am working is fairly simple. So, shouldn't be a problem. If anything happens, there is always a solution B – Zane Jun 18 '17 at 04:05
  • Cheers @JaredFarrish – Zane Jun 18 '17 at 10:43