function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Saravana Bharathi 1Saravana Bharathi 1 

How do i access iframe contentdocument in lwc ?

I want to access iframe content window in lwc. I am getting Secure Element in console. But I am not able to access it. Is there a way to access iframe content window or document once its loaded. What is the Secure Window, Secure Element, Secure Document in Locker API, how can we access those property .

Below are my code:

LwC - HTML file
<template>
<iframe src='https://domain.salesforce.com/home.jsp'/>
</template>

.js file

renderedcallback(){
const iframe = this.template.querySelector('iframe);
iframe.style.background='red';//This is working.
console.log(iframe.contentDocument or iframe.contentWindow) //I am not getting anything in console, but if i use breakpoint, it gives SecureElement in debugger. I could see all the document and DOM Element.

How do i access those element?

Thanks in Advance.

Regards,
Saravana
ShivankurShivankur (Salesforce Developers) 
Hi Saravana,

I looked at this thread(https://github.com/salesforce/lwc/pull/1368) It seems like the suggestion to use iframe like below may work:
<iframe id="same_origin_iframe" src="about:blank"></iframe>
Please check if it works for your use case too.

More reference:
https://github.com/salesforce/lwc/issues/1366

Hope above information helps, Please mark as Best Answer so that it can help others in the future.

Thanks.