Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript, iframe, security - Permission denied when tring to access a js function from parent window

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.

like image 468
zozo Avatar asked May 23 '11 08:05

zozo


2 Answers

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

like image 26
Matej Janovčík Avatar answered Nov 08 '22 06:11

Matej Janovčík


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/.

like image 97
bjornd Avatar answered Nov 08 '22 07:11

bjornd