如何访问内框架 [英] How to access inside frame
问题描述
如何存取iframe:
How to access into iframe:
var iframe = document.getElementById('sitefield1');
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
var elem = innerDoc.getElementsByClassName("myclass")[0];
主页是test1.ru,iframe是test2.ru(都在我的电脑上)。在.htaccess中是
Main page is test1.ru, iframe is test2.ru (both on my computer). In .htaccess is
Header add Access-Control-Allow-Origin "http://test1.ru/2"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
Header add Access-Control-Expose-Headers "X-InlineCount"
在iframe模块中?
我是这个网站的所有者(做一些测试),也可以添加任何设置到我的Firefox。
我发现这个代码不工作在IE(即使我打开acceess在不同的域和XSS过滤器关闭)。 Chrome似乎不支持--disable-web-security ... Chrome的错误是
阻止了一个带有test1的框架访问一个带有test2属性的框架。协议,网域和港口必须匹配。
Is it possible to get inside the iframe module? I am owner of this web sites (do some testing) and can also add any settings to my firefox. What I found also that this code do not work in IE (even that I turn on acceess beween different domains and XSS filter is turned off). Chrome looks like do not support --disable-web-security anymore... Chrome's error is Blocked a frame with origin "test1" from accessing a frame with origin "test2". Protocols, domains, and ports must match.
任何想法都将非常有用。
Any ideas will be very helpful to do this.
推荐答案
您不能直接访问框架。在现代浏览器中,您可以使用 postMessage 。
You can't directly access frames across origins. In modern browsers you can use postMessage.
发送数据的框架需要调用 postMessage
The frame sending the data needs to call postMessage
top.postMessage({ foo: "bar" }, "*");
,并且接收数据的帧需要注册事件侦听器以查找消息并对其作出反应。
and the frame receiving the data needs to register an event listener to look for messages and react to them.
window.addEventListener("message", receiveMessage, false);
function receiveMessage(evt) {
alert(event.data.foo);
}
这篇关于如何访问内框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!