views:

191

answers:

3

Hello, Just a heads up I am a semi noob at JavaScript.

So for the past two days I have been googleing and for the life of me I cannot figure out how to execute a function in an iframe from the parent page. The page in the iframe is on a different domain and the code to be executed is usually executed via a link, but I want it to be executed without have the users click on the link.

I hope that made some sense. If you guys would like to see what I have so far just let me know, but I don't think it would be of much help since it doesn't work at all.

A: 

Different domain? You can't.

David Dorward
+3  A: 

AFAIK, you can not execute JavaScript from a different domain in another fram due to security restrictions in most browsers (definitely Mozilla).

I have heard rumors of a way around that with Yahoo! Pipes but must cofess that I never investigated that so don't know if that is the case.

Just google for "same origin policy workaround" and see if any of the workarounds listed serve your purpose.

DVK
I can execute the script by typing it into the url bar while i ma viewing the page, could I do something similar in an iframe? Thanks for the pipes thing, I will look into it.
Robert Syvarth
No - because when you are typing in the location bar your security permissions are local, whereas the JS executing from the fram has permissions associated with the domain where the web page originated.One thing you could CONSIEVABLY do, would be to read the whole contents of an IFRAME, change the IFRAME's location to a URL on your own domain, and write the contents of the HTML+JS back into that IFRAME. That would only work if JS was embedded and if there are no relative-domain URLs in the page.
DVK
I have gone through that before, but the page contains lots of PHP that is also needed, thus its impossible. I think I am just going to make the iframe scrollto the spot where they need to click and it should work out just fine.Thank you very much for you help, I would probably still be trying to do something which is impossible right now if it wasn't for you.
Robert Syvarth
A: 

https://developer.mozilla.org/en/Same%5Forigin%5Fpolicy%5Ffor%5FJavaScript describes one way of adjusting the domain of a page with limits.

There is one exception to the same origin rule. A script can set the value of document.domain to a suffix of the current domain. If it does so, the shorter domain is used for subsequent origin checks. For example, assume a script in the document at http://store.company.com/dir/other.html executes the following statement:

There are other ways of communicating between frames that don't blur domain boundaries, such as http://ajaxian.com/archives/crossframe-a-safe-communication-mechanism-across-documents-and-across-domains .

Mike Samuel