Get access to iframe content?

1

What kind of friends I hope you can support me with some doubts I have.

I am uploading a page in an iframe , and when I try to access the contentDocument it returns the following error DOMException: Blocked a frame with origin " link "from accessing to cross-origin frame. , I understand that this is normal since they are two different dominions.

I would like to know if there is a way to get access (I mean if you know a js that allows that functionality) or in my case I'm using ASP.NET MVC and if there is an alternative to be able to solve my dilemma.

    
asked by Eduardo 16.11.2017 в 01:48
source

1 answer

1

Try using window.postMessage to send / receive information between sites that do not belong to the same domain.

To send a message from the iframe, it would be:

document.getElementById('iframe').postMessage('hola', '*');

The parameter * indicates that you will send the message to any site or iframe that is listening for a message. Then to receive it on the page would be like this:

window.addEventListener("message", function(e){
   if(e.origin.indexOf('url-de-tu-sitio) > 0)
   {
      // se envio un mensaje desde tu sitio
   }
});

This works both ways, both from your page for the iframe and from the iframe for the page that runs it. Of course, this means that you should have control of the page that will be loaded in the iframe to send the message or you will not be able to.

Currently this is the only way to do it without having to add the CORS origin policies.

    
answered by 16.11.2017 в 04:20