+ 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
<iframe src='https://domain.salesforce.com/home.jsp'/>

.js file

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.

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:

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