0

I want to get Content from a website with an iframe and I'm using basysmith`s answer here(https://stackoverflow.com/a/3127056/8177490) to get the content so that I can serve the iframe from my domain.

I want to display only a certain div, let's call it "table-wrap" from the page and have everything else in the iframe hidden.

I've tried almost every answer on this topic but nothing seems to work. When I am using this

var iframeDoc = document.getElementById('iframeid').contentWindow; 
jQuery(iframeDoc).find('body > not:#table-wrap').hide();
jQuery(iframeDoc).find('#table-wrap').appendTo('body');

in the console.

I am still getting an error, although it is served from my Script on my Domain:

VM4380:66 Uncaught DOMException: Failed to read the 'contentDocument' property from 'HTMLIFrameElement': Blocked a frame with origin "https://mydomain.de" from accessing a cross-origin frame.
at HTMLIFrameElement.contentDocumentDesc.get [as contentDocument] (<anonymous>:66:14)
at contents (https://mydomain.de/wp-includes/js/jquery/jquery.js?ver=1.12.4:2:26888)
at Function.map (https://mydomain.de/wp-includes/js/jquery/jquery.js?ver=1.12.4:2:3613)
at a.fn.init.n.fn.(anonymous function) [as contents] (https://mydomain.de/wp-includes/js/jquery/jquery.js?ver=1.12.4:2:27001)
at <anonymous>:1:18

I've tried also different ways with iframe.onload and

jQuery("#iframeID").ready(function () { 
  jQuery('#iframeid > :not(#table-wrap)').hide(); 
  jQuery('#table-wrap').appendTo('body');
});

But nothing seems to work.

Does anybody have an idea for this how I can get this to work? Would appreciate anything.

digitalsuite.net
  • 339
  • 6
  • 20

1 Answers1

0

Is the location where you are pulling jQuery from (https://mydomain.de) the same as both the outer page and also in the inner iFrame page?

Because the task of actually accessing the iFrame content falls to jQuery in your example, you'll want to verify that all domains (including the jQuery script itself) match to avoid cross domain errors.

Depending on what you're trying to do, window.postMessage may be a better solution anyway as it bypasses a lot domain and security problems (assuming you own both the outer and inner pages), or even just a direct ajax request between the page and your server (thus avoiding the iFrame altogether).

  • Yes they are all the same domains, thats why I was using a serverside script to get the contents(which are from a different domain). However this is described as a working solution in the linked thread. Edit: I only own the outer Domain. I do not own the inner page. – digitalsuite.net Apr 09 '18 at 17:25
  • Just updated my original answer. You might find it easier simply to have the outer page request the content directly from your server using an ajax request and avoid the iFrame altogether. – Steve Griffiths Apr 09 '18 at 17:29
  • I used ajax and filtered the data after the call to only give me what I needed and it worked. Thank you for the idea – digitalsuite.net Apr 09 '18 at 19:33
  • Cool good to hear - in that case would you be happy marking my answer as the solution for your issue? – Steve Griffiths Apr 10 '18 at 08:38