Iframe跨网域问题 [英] Iframe cross domain issue
问题描述
我想在几秒钟内从iframe传送讯息到父页面,例如:
Iframe Domain = www.abc .com
Parent Domain = www.xyz.com
查看:
有人可以帮我吗?
我最近帮助另一个人有一个非常相似的问题,在iframe之间传递消息。 (请参阅 IFrame和父级之间的跨文档消息传递问题)。
使用我在上述主题中从 suamikim 借用的代码的修改版本,定时器。这应该是一个很好的起点。这些父级和子级html页面将按照上述注释 Amadan 在跨网域工作。我刚刚测试并确认,通过在指向 child.html 的一个域上放置 parent.html
b
< html>< head> < script type =text / javascript> function parentInitialize(){var count = 0; window.addEventListener('message',function(e){//检查消息来源等... if(e.data.type ==='iFrameRequest'){var obj = {type:'parentResponse',responseData: Response#'+ count ++}; e.source.postMessage(obj,'*');} // ...})}< / script>< / head>< body style =background-color:rgb (72,222,218); onload =javascript:parentInitialize();> < iframe src =child.htmlstyle =width:500px; height:350px;>< / iframe>< / body>< / html>
$ b b
< html>< head> < script type =text / javascript> function childInitialize(){// ... try {if(window.self === window.top){//我们不是在IFrame里面,不要做任何事情... return; }} catch(e){//由于同源策略,浏览器可以阻止对window.top的访问。 //参见http://stackoverflow.com/a/326076 //如果发生这种情况,我们在一个IFrame ...} function messageHandler(e){if(e.data&&(e.data.type) ==='parentResponse')){//对发送的数据做一些事情var obj = document.getElementById(status); obj.value = e.data.responseData; }} window.addEventListener('message',messageHandler,false); setInterval(function(){window.parent.postMessage({type:'iFrameRequest'},'*');},1000); // ...}< / script>< / head>< body style =background-color:rgb(0,148,255) onload =javascript:childInitialize();> < textarea type =textstyle =width:400px; height:250px; id =status/>< / body>< / html>
祝你好运!
I want to pass a message from iframe to parent page in every few seconds like:
Iframe Domain = www.abc.com
Parent Domain = www.xyz.com
Have check with:
Can someone help me on it?
I just recently helped another person with a very similar concern, passing messages between IFrames. (see Issues with Cross Document Messaging between IFrame & Parent).
Using a modified version of the code that I am borrowing from suamikim in that aforementioned topic, I have integrated a timer. This should serve as a good starting point for you. These parent and child html pages will work cross-domain just as described in the comments above by Amadan. I just tested and confirmed it, by placing parent.html on one domain, which pointed to child.html on a totally separate untrusted domain.
parent.html
<html>
<head>
<script type="text/javascript">
function parentInitialize() {
var count = 0;
window.addEventListener('message', function (e) {
// Check message origin etc...
if (e.data.type === 'iFrameRequest') {
var obj = {
type: 'parentResponse',
responseData: 'Response #' + count++
};
e.source.postMessage(obj, '*');
}
// ...
})
}
</script>
</head>
<body style="background-color: rgb(72, 222, 218);" onload="javascript: parentInitialize();">
<iframe src="child.html" style="width: 500px; height:350px;"></iframe>
</body>
</html>
child.html
<html>
<head>
<script type="text/javascript">
function childInitialize() {
// ...
try {
if (window.self === window.top) {
// We're not inside an IFrame, don't do anything...
return;
}
} catch (e) {
// Browsers can block access to window.top due to same origin policy.
// See http://stackoverflow.com/a/326076
// If this happens, we are inside an IFrame...
}
function messageHandler(e) {
if (e.data && (e.data.type === 'parentResponse')) {
// Do some stuff with the sent data
var obj = document.getElementById("status");
obj.value = e.data.responseData;
}
}
window.addEventListener('message', messageHandler, false);
setInterval(function () {
window.parent.postMessage({ type: 'iFrameRequest' }, '*');
}, 1000);
// ...
}
</script>
</head>
<body style="background-color: rgb(0, 148, 255);" onload="javascript: childInitialize();">
<textarea type="text" style="width:400px; height:250px;" id="status" />
</body>
</html>
Good luck!
这篇关于Iframe跨网域问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!