不同网站的 Iframe

Iframe of different website

本文关键字:Iframe 网站      更新时间:2023-09-26

我需要检查父 iframe 网站是否与内部 iframe 不同

var obj = parent.location.href;
if(typeof obj !== "undefined")
{
      //different
}
elss
{
     //same
}

这适用于铬...但是在火狐/即上给出和错误(访问属性"tostring"的权限被拒绝)

请提出替代解决方案

即使允许跨源访问,这也有效。如果不允许,请使用 try catch 禁止显示错误。

var same = false;
try{
    var parentLocation = parent.location.href;
    if(parentLocation == location.href){
        same = true;
    }
}catch(e){
    //error => permission denied => different website
}