简体   繁体   English

跨域iframe问题

[英]Cross domain iframe issue

For say i have a Site called example.com on which iframe is embedded of domain iframe.net, now i want to read the content of iframe and pass some parameter to display a textual message.例如,我有一个名为 example.com 的站点,其中 iframe 嵌入了域 iframe.net,现在我想阅读 iframe 的内容并传递一些参数以显示文本消息。 Like Hi with username.喜欢用户名的嗨。

Now the problem is this able not able to make connection between the two, even am not able to get the innerHTML of iframe i used following approach现在的问题是这无法在两者之间建立联系,甚至无法获得 iframe 的内部HTML 我使用了以下方法

document.getElementById('myframe').contentWindow.document.body.innerHTML;

It throws error "Permission denied to access property"它抛出错误“访问属性的权限被拒绝”

Do anyone know how to read and write in cross domain platform有谁知道跨域平台读写

If you don't have control over the framed site, you cannot circumvent the cross-domain policy.如果您无法控制框架站点,则无法绕过跨域策略。

If you have control over both sites, you can use the postMessage method to transfer data across different domains.如果您可以控制这两个站点,则可以使用postMessage方法跨不同域传输数据。 A very basic example:一个非常基本的例子:

// framed.htm:
window.onmessage = function(event) {
    event.source.postMessage(document.body.innerHTML, event.origin);
};

// Main page:
window.onmessage = function(event) {
    alert(event.data);
};

// Trigger:
// <iframe id="myframe" src="framed.htm"></iframe>
document.getElementById('myframe').contentWindow.postMessage('','*');

In Internet Explorer 8 , events passed as a parameter may be null , that is why you need to access the event in a different manner:Internet Explorer 8 中,作为参数传递的事件可能为null ,这就是您需要以不同方式访问事件的原因:

In frame.html :frame.html 中

window.onmessage = function(event) {
   var evt = event || window.event;
   evt.source.postMessage('Message from iFrame', evt.origin);
};

On main.html :main.html 上

window.onmessage = function(event) {
   var evt = event || window.event;
   alert(evt.data);
};

The event is triggered the same way as Rob W has presented:该事件的触发方式与 Rob W 介绍的相同:

document.getElementById('frameId').contentWindow.postMessage('message','*');

iFrame does not allow to access contents from Cross Domain platform. iFrame 不允许从跨域平台访问内容。 You can only access if your iFrame is using the same domain.您只能在 iFrame 使用相同域时访问。

This solution works same as iFrame.此解决方案的工作原理与 iFrame 相同。 I have created a PHP script that can get all the contents from the other website, and most important part is you can easily apply your custom jQuery to that external content.我创建了一个 PHP 脚本,可以从其他网站获取所有内容,最重要的部分是您可以轻松地将自定义 jQuery 应用于该外部内容。 Please refer to the following script that can get all the contents from the other website and then you can apply your cusom jQuery/JS as well.请参考以下脚本,可以从其他网站获取所有内容,然后您也可以应用您的自定义 jQuery/JS。 This content can be used anywhere, inside any element or any page.此内容可以在任何元素或任何页面内的任何地方使用。

<div id='myframe'>

  <?php 
   /* 
    Use below function to display final HTML inside this div
   */

   //Display Frame
   echo displayFrame(); 
  ?>

</div>

<?php

/* 
  Function to display frame from another domain 
*/

function displayFrame()
{
  $webUrl = 'http://[external-web-domain.com]/';

  //Get HTML from the URL
  $content = file_get_contents($webUrl);

  //Add custom JS to returned HTML content
  $customJS = "
  <script>

      /* Here I am writing a sample jQuery to hide the navigation menu
         You can write your own jQuery for this content
      */
    //Hide Navigation bar
    jQuery(\".navbar.navbar-default\").hide();

  </script>";

  //Append Custom JS with HTML
  $html = $content . $customJS;

  //Return customized HTML
  return $html;
}

Cross Domain iframe elements access in React with (srcDoc):- React 中的跨域 iframe 元素访问(srcDoc):-

<iframe srcDoc={this.state.HTML}  width='100%' id='iframetnd' onLoad={this.onclickinsideiframe}/>


 onclickinsideiframe=()=>{
      if(document.getElementById('iframetnd')!==null){

                let iframe = document.getElementById('iframetnd');

                let innerDocument = (iframe.contentDocument)  
                                   ? iframe.contentDocument 
                                   : iframe.contentWindow.document;
                let Obj = innerDocument.getElementsByClassName("navButtons")[0];
                    if(Obj !== undefined){
                        Obj.onclick = ()=>this.func();
          }
       }
    }



  func=()=>{
           this.setState({page:2})
           this.arrangeTest();
        }

It can happen only if you have control for both sites只有当您对两个站点都拥有控制权时才会发生

you can use postMessage你可以使用postMessage

here's how you can apply it这是您如何应用它

first, you can add the below code to the site you want to retrieve data from首先,您可以将以下代码添加到要从中检索数据的站点

 <script> function test() { document.getElementById('idMyIframe').contentWindow.postMessage("your message here", "*"); } </script> <iframe id="idMyIframe" onload="test()" src="http://example.otherdomaintosenddatato.com"></iframe>

the second step you can add this code below to the other domain you want to send a message or the data to it第二步,您可以将此代码添加到您要向其发送消息或数据的其他域中

 window.addEventListener("message", function (message) { if (message.origin == "http://firstdomain.com") { console.log(message) } });

and please note you must add the test() function on the onload attribute of the iframe cause if the function runs before the iframe was loaded it will become useless and it won't send data请注意,您必须在 iframe 的 onload 属性上添加 test() 函数,因为如果该函数在 iframe 加载之前运行,它将变得无用并且不会发送数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM