Foolproof way to detect if this page is INSIDE a cross-domain iframe

后端 未结 8 1103
梦如初夏
梦如初夏 2020-12-05 13:37

An answer to \"Foolproof way to detect if iframe is cross domain\" describes a method to test if an iframe on a page points to a same-domain or cross-domain page, working ar

8条回答
  •  孤街浪徒
    2020-12-05 14:24

    Real cross-browser solution for posterity:

    function isCrossOriginFrame() {
      try {
        return (!window.top.location.hostname);
      } catch (e) {
        return true;
      }
    }
    
    console.log(isCrossOriginFrame());
    

    Tested on a decent swath of desktop and mobile browsers.

    https://jsfiddle.net/arzd4btc/3/

提交回复
热议问题