Iframe跨网域问题

编程入门 行业动态 更新时间:2024-10-27 06:27:58
本文介绍了Iframe跨网域问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在几秒钟内从iframe传送讯息到父页面,例如:

Iframe Domain = www.abc Parent Domain = www.xyz

查看:

  • 跨网域iframe问题
  • jQuery Cross-iFrame脚本加载(ownerDocument Issue)

有人可以帮我吗?

解决方案

我最近帮助另一个人有一个非常相似的问题,在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的访问。 //参见stackoverflow/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 Parent Domain = www.xyz

Have check with:

  • Cross domain iframe issue
  • jQuery Cross-iFrame Script loading (ownerDocument Issue)

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 stackoverflow/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跨网域问题

本文发布于:2023-08-06 04:52:14,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Iframe   跨网域

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!