3

I have an iframe which displays pdf file embed into it and I couldn't access the iframe element contents.

<iframe id="testIFrame" src="http://localhost:8080/myproject/pdf/sample.pdf" width="800" onload="test(this)"></iframe>

The following code in firebug console,

var iframe = document.getElementById('testIFrame');
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
console.log("Iframe Document"+innerDoc);

gives me error,

Error: Permission denied to access property "document"

The same iframe can be accesible when I have an image file as URL. The cross domain error occurs only for pdf files. How can I solve this?

EDIT:

The URL that I'm having this iframe is http://localhost:8080/myproject and the iframe source point to http://localhost:8080/myproject/pdf/sample.pdf. And I can see in the Firebug Net panel that the pdf request is having

Protocol - http,

Domain - localhost:8080, and

RemoteIP - [::1]:8080

And the request headers contains the refer url as http://localhost:8080/denso/

Am I missing something in the Same Origin Policy?

The reason because, only FF shows a tool bar to download, print etc for pdf files embeded in iframe which can be disable by adding param #toolbar=0 at the end of the pdf url which works in chrome. If I can access the iframe contents I can hide the toolbar with css based on this link

https://support.mozilla.org/en-US/questions/1119523

Lucky
  • 16,787
  • 19
  • 117
  • 151
  • Besides protocol, sub-domain, and domain needing to be the same for Same Origin Policy, it also requires that Port numbers match as well. – zer00ne Jul 30 '16 at 23:09
  • You can refer to this: http://stackoverflow.com/questions/7995223/error-permission-denied-to-access-property-document – Sky Jul 31 '16 at 00:20
  • @zer00ne both are same protocol, domain, and port. `http://localhost:8080/myproject/pdf/sample.pdf` and the page I'm trying to access the contents is `http://localhost:8080/myproject`. I don't find any mismatch in this. And this only happens for pdf file. I can able to access other resources like css, js, image files etc. – Lucky Aug 01 '16 at 07:34
  • @Sky The domain, port and host are the same. But it show the permission denied message. – Lucky Aug 01 '16 at 08:26
  • Are you only using Firefox? Have you tried Chrome? What type of local server are you using? – zer00ne Aug 01 '16 at 18:37
  • @zer00ne I need this to work on FF especially. `document.getElementById('testIFrame').contentDocument;` works fine in chrome. The reason because, only FF shows a tool bar to download, print etc for pdf files embeded in iframe which can be disable by adding `#toolbar=0` which works in chrome. If I can access the iframe contents I can hide the toolbar with css. But since it gives error I of no help in doing that in FF. – Lucky Aug 02 '16 at 17:00

0 Answers0