如何处理由于交叉原点导致的安全性错误

时间:2014-05-19 20:03:01

标签: javascript

Uncaught SecurityError:无法从'HTMLIFrameElement'中读取'contentDocument'属性:阻止具有原点“http://www.bhjk.com”的框架访问具有原点“http://c.betrad.com”的框架。协议,域和端口必须匹配。

Heres'我的代码:

 getBody: function(slot) {

    //Here I'm checking if div id is null to avoid type errors later
    if(document.getElementById(this.getDomId(slot)) != null){
    var iframe = document.getElementById(this.getDomId(slot)).getElementsByTagName('iframe')[0];
    console.log("iframe is:");
    console.log(iframe);
    }
    else{
    return false;
    }
      console.log("now checking for same origin policy");
console.log(iframe.contentDocument.body);
    if(iframe) return iframe.contentDocument.body;
        return false;
    }, 

更具体地说,以下陈述给我带来了问题。 Wahts是处理上述错误的最佳方法吗?

if(iframe) return iframe.contentDocument.body;

1 个答案:

答案 0 :(得分:0)

也许有一个try-catch块:

try{
   return iframe.contentDocument.body;
}catch(exception){
  console.log(exception);
  return false;
}