How to identify if a webpage is being loaded inside an iframe or directly into the browser window?

前端 未结 16 2342
攒了一身酷
攒了一身酷 2020-11-22 00:28

I am writing an iframe based facebook app. Now I want to use the same html page to render the normal website as well as the canvas page within facebook. I want to know if I

16条回答
  •  天涯浪人
    2020-11-22 01:07

    When in an iframe on the same origin as the parent, the window.frameElement method returns the element (e.g. iframe or object) in which the window is embedded. Otherwise, if browsing in a top-level context, or if the parent and the child frame have different origins, it will evaluate to null.

    window.frameElement
      ? 'embedded in iframe or object'
      : 'not embedded or cross-origin'
    

    This is an HTML Standard with basic support in all modern browsers.

提交回复
热议问题