Murali krishna Konduru
Murali krishna Konduru

Reputation: 191

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

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.

Upvotes: 0

Views: 388

Answers (1)

aweebit
aweebit

Reputation: 646

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();

Upvotes: 1

Related Questions