cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Lampe2020
Making moves
Status: New idea

I want to be able to easily and reliably check if a javascript is run inside an iframe, a bit like:

"
function onlyRunWhenImInsideIframe() {
    insideIframe = document.checkIfInsideIframe();
    console.log(insideIframe); // should return true or false
    if (insideIframe) { /* run other code */ }
}
"

I think it would be much easier with such a function to make a site a combined site, so if it's loaded directly it displays fully but if it's loaded inside an iframe it shows the embed version.

4 Comments
Status changed to: New idea
Jon
Community Manager
Community Manager

Thanks for submitting an idea to the Mozilla Connect community! Your idea is now open to votes (aka kudos) and comments.

sfoster
Employee
Employee

It sounds like you want window.top which lets you know this.

Lampe2020
Making moves

I've heard window.top doesn't always work, for example when the IFrame and the page in which it's embedded aren't from the same origin, in such a case you have to do error handling and all that, so it would be easier if there was a reliable function that even works cross-origin (that would be possible because the requesting JS isn't accessing the properties of any page but only starting a function) and only returns true or false.

jscher2000
Leader

Mozilla isn't very likely to create new JavaScript methods that aren't part of a standard, unless there are very unusual circumstances that require it. For now, you can compare `window.self === window.top` in your pages to check the embeddedness (if that is word). For example:

https://www.jeffersonscher.com/res/xsiteframe.html