Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append html content into iframe?

I tried the code below but it completely replaces the content inside the iframe:

@ViewChild('iframe') iframe: ElementRef;

let content = '<h1>TEST CONTENT</h1>';
let doc = this.iframe.nativeElement.contentDocument;
doc.open();
doc.write(content);
doc.close();

How do I append html content into the iframe without replacing?

like image 874
Ahmet Ömer Avatar asked May 29 '26 03:05

Ahmet Ömer


1 Answers

this.iframe.nativeElement.contentWindow.frames.document.body.insertAdjacentHTML('beforebegin', contentToAppend);

this worked for me. I get the iframe element in Angular with ElementRef & @ViewChild then dig down to it's HTML body and append HTML content using insertAdjacentHTML method, in the position desired, available as beforebegin, afterbegin, beforeend and afterend.

like image 185
Ahmet Ömer Avatar answered May 30 '26 18:05

Ahmet Ömer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!