Onload event is not firing after passing Html string through window.open in Angular 8

Issue

I have a scenario, I am receiving Html comtent as a string from java service and I am trying to render this through Angular using window.open method. Html elements are data is diplaying, but while loading window javascript onload event is not triggering.

Html string receiving from java:

<html><h1>Workflow Detailed Report modified</h1><head><script type=text/javascript>function loadReport() {alert('Load Report'); window.open("http://google.com","_self")}</script></head><body onload="loadReport()"></body></html>

In Angular function using below code to open new window with above string.

const win = window.open('', '_blank', 'left=50,top=50,width=1334,height=700');
win.document.write(htmlData);

Is there any better way to handle this scenario in Angular 8.

Solution

When using document.write(), you have to call document.close() afterwards to finalize the document, otherwise the load event will not be fired.

const win = window.open('', '_blank', 'left=50,top=50,width=1334,height=700');
win.document.write(htmlData);
win.document.close();

Answered By – aweebit

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published