0

I want to get contents of iframe, but an error occurred.

Error: Permission denied to access property 'document'

...irstChild)},contents:function(a){return m.nodeName(a,"iframe")?a.contentDocument...

my code :

<iframe frameborder="0" scrolling="no" width="130" height="198"
   src="https://tpc.googlesyndication.com/simgad/9598136166282506848" name="imgbox" class="iView">
   <p>iframes are not supported by your browser.</p>
</iframe>


$(document).ready(function(){
    var cnt = $(".iView").contents();
    console.log(cnt);
    })
Rohan Kushwaha
  • 738
  • 6
  • 18
Zisu
  • 497
  • 2
  • 6
  • 25
  • seems like you are opening third party website inside iFrame and trying to access its its document. You can't do that. – vijayP Sep 07 '15 at 11:04
  • Is there any way to get iframe contents??? – Zisu Sep 07 '15 at 11:07
  • If iFrame has `src` pointing to the web resource present within your own domain i.e. the domain of main parent page then you can access its content. – vijayP Sep 07 '15 at 11:09

4 Answers4

1

The same origin policy restricts you from accessing the DOM in the iframe contents. However, it does not prevent you from barely embedding the page.

For more details about same origin policy, and ways to work around it, please see the following community wiki: Ways to circumvent the same-origin policy

Community
  • 1
  • 1
FelisCatus
  • 5,054
  • 2
  • 21
  • 25
0

Through Javascript you can access the iFrame content if that is from the same server. Else it will not allow you to access the iFrame content. Error: Permission denied to access property 'document'.

0

Actually this can not be done when you are doing it from cross domain. You will get permission denied error. When the Iframe source is coming from the same server only then this can be done.

0

I create a sample code . Now you can easily understand from different domain you can't access content of iframe .. Same domain we can access iframe content

I share you my code , Please run this code check the console . I print image src at console. There are four iframe , two iframe coming from same domain & other two from other domain(third party) .You can see two image src( https://www.google.com/logos/doodles/2015/googles-new-logo-5078286822539264.3-hp2x.gif

and

https://www.google.com/logos/doodles/2015/arbor-day-2015-brazil-5154560611975168-hp2x.gif ) at console and also can see two permission error( 2 Error: Permission denied to access property 'document'

...irstChild)},contents:function(a){return m.nodeName(a,"iframe")?a.contentDocument...

) which is coming from third party iframe.

<body id="page-top" data-spy="scroll" data-target=".navbar-fixed-top">
<p>iframe from same domain</p>
  <iframe frameborder="0" scrolling="no" width="500" height="500"
   src="iframe.html" name="imgbox" class="iView">

</iframe>
<p>iframe from same domain</p>
<iframe frameborder="0" scrolling="no" width="500" height="500"
   src="iframe2.html" name="imgbox" class="iView1">

</iframe>
<p>iframe from different  domain</p>
 <iframe frameborder="0" scrolling="no" width="500" height="500"
   src="https://www.google.com/logos/doodles/2015/googles-new-logo-5078286822539264.3-hp2x.gif" name="imgbox" class="iView2">

</iframe>

<p>iframe from different  domain</p>
 <iframe frameborder="0" scrolling="no" width="500" height="500"
   src="http://d1rmo5dfr7fx8e.cloudfront.net/" name="imgbox" class="iView3">

</iframe>

<script type='text/javascript'>


$(document).ready(function(){
    setTimeout(function(){


        var src = $('.iView').contents().find(".shrinkToFit").attr('src');
    console.log(src);
         }, 2000);


    setTimeout(function(){


        var src = $('.iView1').contents().find(".shrinkToFit").attr('src');
    console.log(src);
         }, 3000);


    setTimeout(function(){


        var src = $('.iView2').contents().find(".shrinkToFit").attr('src');
    console.log(src);
         }, 3000);

         setTimeout(function(){


        var src = $('.iView3').contents().find("img").attr('src');
    console.log(src);
         }, 3000);


    })


</script>
</body>
Zisu
  • 497
  • 2
  • 6
  • 25