30

How can I fix this message in Firefox? I am using an Iframe which has an anchor tag? I would like to get a reference to this anchor but i am getting this error when I am trying to access anchor:

var frameWindow = document.getElementById('myIframe').contentWindow;
var anchor = frameWindow.document.links[0]; //.getElementsByClassName('a');
anchor.onclick....
weynhamz
  • 1,968
  • 18
  • 18
user603007
  • 11,416
  • 39
  • 104
  • 168
  • 2
    You can't do that if the frame points to different domain. – Shadow The GPT Wizard Nov 03 '11 at 12:57
  • Seems the iframe is not from the same domain as the parent page. You cannot access the content then. – Felix Kling Nov 03 '11 at 12:57
  • 2
    you can use Firebug CD command for development if it's on another domain: https://groups.google.com/forum/?fromgroups=#!topic/firebug/DvUvoLw-hOI (http://getfirebug.com/wiki/index.php/Command_Line_API#cd.28window.29) – baptx Feb 06 '13 at 19:16
  • 1
    I had this issue with the same domain when one was on HTTPS and the other was on HTTP. The fix was to make them both the same. – robbrit Feb 07 '13 at 16:33
  • See here: http://stackoverflow.com/questions/7961229/is-there-a-way-to-change-context-to-iframe-in-javascript-console – 0fnt Oct 16 '14 at 04:20

3 Answers3

21

Relaxing the same-origin policy

In some circumstances the same-origin policy is too restrictive, posing problems for large websites that use multiple subdomains. Here are four techniques for relaxing it:

document.domain property

If two windows (or frames) contain scripts that set domain to the same value, the same-origin policy is relaxed for these two windows, and each window can interact with the other. For example, cooperating scripts in documents loaded from orders.example.com and catalog.example.com might set their document.domain properties to “example.com”, thereby making the documents appear to have the same origin and enabling each document to read properties of the other. This might not always work as the port stored in the internal representation can become marked as null. In other words example.com port 80 will become example.com port null because we update document.domain. Port null might not be treated as 80 ( depending on your browser ) and hence might fail or succeed depending on your browser.

Cross-Origin Resource Sharing

The second technique for relaxing the same-origin policy is being standardized under the name Cross-Origin Resource Sharing. This draft standard extends HTTP with a new Origin request header and a new Access-Control-Allow-Origin response header. It allows servers to use a header to explicitly list origins that may request a file or to use a wildcard and allow a file to be requested by any site. Browsers such as Firefox 3.5 and Safari 4 use this new header to allow the cross-origin HTTP requests with XMLHttpRequest that would otherwise have been forbidden by the same-origin policy.[7]

Cross-document messaging

Another new technique, cross-document messaging allows a script from one page to pass textual messages to a script on another page regardless of the script origins. Calling the postMessage() method on a Window object asynchronously fires an "onmessage" event in that window, triggering any user-defined event handlers. A script in one page still cannot directly access methods or variables in the other page, but they can communicate safely through this message-passing technique.

JSONP

JSONP allows a page to receive JSON data from a different domain by adding a <script> element to the page which loads a JSON response from a different domain.

The function call is the "P" of JSONP—the "padding" around the pure JSON, or according to some the "prefix". By convention, the browser provides the name of the callback function as a named query parameter value, typically using the name jsonp or callback as the named query parameter field name, in its request to the server, e.g.,

<script type="application/javascript"
        src="http://server2.example.com/Users/1234?jsonp=parseResponse">
</script>

In this example, the received payload would be:

parseResponse({"Name": "Foo", "Id": 1234, "Rank": 7});
Cees Timmerman
  • 17,623
  • 11
  • 91
  • 124
9

If the iframe points to a different domain, you will get this error. This is an example of your browser preventing cross-site scripting: http://en.wikipedia.org/wiki/Cross-site_scripting

Trevor
  • 6,659
  • 5
  • 35
  • 68
  • 35
    That's fine and dandy, but the OP wants to know how to get around it. – Noz Jan 30 '13 at 18:25
  • 5
    @TarynEast Baptx's comment is a much better answer that this, at least it attempts to offer a solution. Though I might even suggest something like easyXDM. – Noz Mar 20 '13 at 16:21
  • @Noz Firebug CD command mentioned in my comment cannot be used anymore but I shared a new workaround: https://stackoverflow.com/questions/7995223/error-permission-denied-to-access-property-document/69138808#69138808 – baptx Sep 10 '21 at 23:16
0

The error is due to the same-origin policy like explained in other answers. I post this answer as a workaround to execute JavaScript code in a web console.

My comment suggesting to use Firebug CD command no longer works because Firebug is not supported anymore.

But there is a similar feature in Firefox Developer Tools, you can switch the domain name by selecting the iframe context picker button like described here: https://developer.mozilla.org/en-US/docs/Tools/Working_with_iframes

baptx
  • 3,428
  • 6
  • 33
  • 42