7

Good day to all.

I have a page that includes an iframe. In that iframe I have a script with function called test(). I need to access the function from the parent window. After asking around I got to this solution:

<div onclick="document.getElementById('targetFrame').contentWindow.teste();">Test</div>

On click the test function should be run. The problem is that I get " Permission denied to access property test" error.

It looked like a permission error to me so I changed the file loaded in iframe permissions to 777, but without any result.

Note: The file loaded in iframe is not on the same domain.

zozo
  • 8,230
  • 19
  • 79
  • 134
  • 1
    this has been asked many times before: http://stackoverflow.com/questions/364952/jquery-javascript-accessing-contents-of-an-iframe – ashwoods May 23 '11 at 08:26

2 Answers2

10

It's prohibited to access pages from other domains by default, because browsers use same origin policy. There are several workaround like using location.hash or window.name to communicate between frames. The most recent and standardized in HTML5 is postMessage-interface. There is library for cross-browser solution http://easyxdm.net/wp/.

bjornd
  • 22,397
  • 4
  • 57
  • 73
  • 1
    Ok... how do I change this? I have root access on both domains. – zozo May 23 '11 at 08:18
  • is this for an intranet, where you have control over all the browsers? – ashwoods May 23 '11 at 08:22
  • if this is the case, you can disable web security in some browsers. search for disabling SOP: http://stackoverflow.com/questions/330427/can-i-disable-sop-same-origin-policy-on-any-browser-for-development, but in general only ugly hacks exists, the security policies implemented on browsers are there for a reason – ashwoods May 23 '11 at 08:31
  • What if I use a subdomain for the iframe file? – zozo May 23 '11 at 08:32
3

I am not sure if it is possible, cross window (frame) communication have to be at same domain, protocol and hostname. For more info see Same origin policy for JavaScript and Cross domain communication with iframes

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Matej Janovčík
  • 1,242
  • 12
  • 13