typescript - How to wait for HTML document to load/render before printing it (plain JavaScript) -


i using code open , print image on new html page:

printview(datauri:string){    var win = window.open();   win.document.write( '<img src="' + datauri + '">');    win.print();   win.close();  } 

when used , image larger few kb, print preview opens blank page, because document not rendered when print invoked.

i solved (temporarily) introducing settimeout ~200ms delay before printing, this:

settimeout( () => {      win.print();      win.close(); }, 200); 

and works, i aware should use dom/window event wait until content loaded. one?

what tried far:

win.document.onload = ()=>{     console.log('event fired'); // never fired     win.print();     win.close(); } 

and

win.addeventlistener('load', ()=>{     console.log('event fired'); // never fired     win.print();     win.close(); } 

i keep vanilla js, please not refer me jquery window.ready.

add onload attribute image

 win.document.write( '<img src="' + datauri + '" onload="print()">'); 

remove win.print() , win.close() printview()

and add them function print()

print() fired when image finished loading.

this time hope works


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -