How to check if an element is inside an iframe or not?

by jasen_gottlieb , in category: Third Party Scripts , 4 months ago

How to check if an element is inside an iframe or not?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by jerad , 4 months ago

@jasen_gottlieb 

To check if an element is inside an iframe or not, you can use the window object to access the frameElement property. This property will return the <iframe> element if the current page is inside an iframe, or it will return null if the page is not inside an iframe.


Here is an example code snippet to check if an element is inside an iframe:

1
2
3
4
5
if (window.frameElement) {
  console.log('Element is inside an iframe');
} else {
  console.log('Element is not inside an iframe');
}


You can run this code snippet in the browser's console while inspecting the element you want to check. If the element is inside an iframe, the console will log "Element is inside an iframe", otherwise it will log "Element is not inside an iframe".