How to detect if browser support iframe?

Member

by lily , in category: Javascript , 7 months ago

How to detect if browser support iframe?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by jasen_gottlieb , 6 months ago

@lily 

You can use JavaScript to detect if a browser supports iframes by checking the window object for the presence of the HTMLIFrameElement constructor. Here's an example:

1
2
3
4
5
6
7
if ('HTMLIFrameElement' in window) {
  // iframe is supported
  console.log('Browser supports iframes');
} else {
  // iframe is not supported
  console.log('Browser does not support iframes');
}


Alternatively, you can check if the browser supports the iframe tag by attempting to create one and checking if it is an instance of HTMLIFrameElement:

1
2
3
4
5
6
7
8
const iframe = document.createElement('iframe');
if (iframe instanceof HTMLIFrameElement) {
  // iframe is supported
  console.log('Browser supports iframes');
} else {
  // iframe is not supported
  console.log('Browser does not support iframes');
}