How to check iframe is empty?

Member

by jasen , in category: Javascript , 17 days ago

How to check iframe is empty?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by hal.littel , 16 days ago

@jasen 

To check if an iframe is empty, you can use JavaScript to check if it has any content inside it. Here is a simple example to demonstrate how you can achieve this:

  1. Get a reference to the iframe element using document.querySelector or any other method that suits your needs:
1
const iframe = document.querySelector('iframe');


  1. Check if the iframe has any content by accessing its contentDocument property and checking if it is null:
1
2
3
4
5
if (iframe && !iframe.contentDocument) {
  console.log('The iframe is empty');
} else {
  console.log('The iframe is not empty');
}


This code snippet will check if the iframe is empty or not based on whether its contentDocument property is null or not. You can further refine this check based on your specific requirements.