Niks Niks - 5 months ago 18
Javascript Question

Javascript: Empty html div contents after Window.print()

On button click, I'm using

window.print()
of javascript for printing
print_div
contents,

But because of some security issue, i wanted to avoid the multiple printing (like using Cntrl + P) options, So i was thinking of to clear
print_div
contents, that users can't re-print again and again.

Rough Code here,

document.getElementById("print_div").innerHTML = //Some contents to be printed on paper.
window.print()
document.getElementById("print_div").innerHTML = '' // Clearing old contents to avoid mis-use


But this code not working at all, its clearing
print_div
contents before creating print preview like in chrome.(i guessed, working Asynchronously)

Could any one tell me, where am doing wrong here?

Note: Am using
Chrome: 22.0.1229.92 m
to test my code & i want to go for chrome only.

Answer

You are printing in the print_div and in next step you are clearing the same. Therefore it shows empty. You can store printing data in a variable. Then display it in the print_div and you can clear the variable.

var print = "";
print += "<p>Something </p><br/>";
print += "<table><tr><td>Something</td><td>Something</td></tr> 
                <tr><td>Something</td><td>Something</td></tr> </table>";
document.getElementById("print_div").innerHTML = print;
print = "";

something like this. Set this javascript to button onsubmit event.