Use IFrame in LWC Salesforce

by Rijwan Mohmmed
0 comment
use-iframe-in-lwc-salesforce

Hello friends, today we will discuss Use IFrame in LWC component in Salesforce. We use an iframe to load other website pages. Like if you want to show third-party page eg. google.com then you just need to put this URL link in iframe src. We can give height and width in the iframe. We also use event listeners like onload for the call LWC js method.

Also check this: Test Data Factory in Apex Salesforce

Key Highlights :

  1. We use the iframe to display visualforce page where we can send parameters in URL and the page load accordingly.
  2. We also use iframe for generating pdf.
  3. Load third-party web page
  4. we need to put a third-party URL in CSP security so the page can load in Salesforce.

Code :

First, we will store our website link in CSP Trusted Sites.

IFrame in LWC Salesforce
IFrame in LWC Salesforce

lWCIFrame.HTML :

<template>
  <iframe
    src={url}
    height={height}
    width={width}
    referrerpolicy={referrerPolicy}
    sandbox={sandbox}
    title="techdicer"
  ></iframe>
</template>

lWCIFrame.JS :

import { LightningElement, track } from 'lwc';
export default class LWCIFrame extends LightningElement {
    @track height = '900px';
    @track referrerPolicy = 'no-referrer';
    @track sandbox = '';
    @track url = 'https://techdicer.com/';
    @track width = '100%';
}

lWCIFrame.js-meta.xml :

<?xml version="1.0"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
	<apiVersion>54.0</apiVersion>
	<isExposed>true</isExposed>
	<targets>
		<target>lightning__HomePage</target>
	</targets>
</LightningComponentBundle>

Output :

use-iframe-in-lwc-salesforce-output

Reference :

  1. LWC

You may also like

Leave a Comment