Executing Javascript in iFrame from Parent

2019-08-07 10:28发布

问题:

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.

回答1:

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.



回答2:

Different domain? You can't.



回答3:

https://developer.mozilla.org/en/Same_origin_policy_for_JavaScript 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 .