0

i want to call a javascript function on another webpage. So far, my solution is to create a page with an iframe containing the target page.

<a onclick="func1()">Call</a>
    <script type="text/javascript">
        function func1() 
        {
            document.getElementById('myFrame').contentWindow.some_function();
        }
    </script>
<iframe id="myFrame" src="http://www.webpage.com">

Problem is, that this solution only works when both pages have the same domain. Otherwise, i am getting this error:

Uncaught SecurityError: Blocked a frame with origin "http://my_webpage.com" from accessing a frame with origin "http://not_my_webpage.com". Protocols, domains, and ports must match. 

I have googled for hours, but i have only found that i need to use

document.domain = document.domain;

After using it, i get this error:

Uncaught SecurityError: Blocked a frame with origin "http://my_webpage.com" from accessing a frame with origin "http://not_my_webpage.com". The frame requesting access set "document.domain" to "http://my_webpage.com", but the frame being accessed did not. Both must set "document.domain" to the same value to allow access. 

I guess that i need to use it on both pages, but the page in iframe isn't mine and i don't have acces to it. Any ideas? Thanx.

1
  • 1
    It is not possible, this security mechanism was added to exactly prevent this by default. Commented Aug 3, 2014 at 11:51

1 Answer 1

2

You just can't do it. Imagine if this kind of things were possible... it would be an enormous security flaw. That's because you can't access a frame that has another origin, the browser automatically blocks every script that tries to access some window that hasn't the same origin.

Sign up to request clarification or add additional context in comments.

3 Comments

Well, there's CORS, which can allow this (but requires the external server to allow it, not the originating service/web-page).
Yeah I know, but find me some site that uses it... one in one billion, maybe. Since that the OP has no control on the foreign page he can't do anything.
Alternatively, you can send messages between pages on different domains using Window.postMessage().

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.