2014-09-29 110 views
1

如何获得iframe中的iframe属性的JavaScript代码获取iframe中的iframe JavaScript和跨域

<div> 
<iframe src="http://192.168.0.108:8092/" 
      name="otherDomain" 
      width="100%" 
      height="600px" 
      style="border: 0;" 
      data-auth-token="xyz" 
      data-auth-key="abc" 
     > 
</div> 

我与

console.log(window.frameElement.getAttribute('data-auth-token')) 

试图属性如果iframe网址和浏览器的用户是相同的,我能够获取iframe属性。如果网址不同,我会收到以下错误消息。

Uncaught SecurityError: Failed to read the 'frame' property from 'Window': Blocked a frame with origin "http://192.168.0.108:8092" from accessing a frame with origin "http://localhost:8092". Protocols, domains, and ports must match. 

是否有可能在JavaScript中获得跨域iframe属性。

回答

2

不幸的是,您不可能使用引用其他域的iframe。浏览器会自动阻止所有尝试从另一个域访问帧的脚本。如果这样的事情是可能的,那么这将是一个巨大的安全缺陷。

在你的情况,你可以将iframe属性设置srclocalhost:8092,所以你必须以相同的位置(和相同的域名)的iframe,你就可以访问其数据。

+0

感谢您的解释@Marco Bonelli。我想错了。 – Prasanna 2014-09-29 08:50:29