开发者

Javascript call function from an external domain iframe

开发者 https://www.devze.com 2023-03-29 08:31 出处:网络
Page A 开发者_开发问答and page B are on different domains. A is iframed on page B. I need to call a function which is on page B from page A.

Page A 开发者_开发问答and page B are on different domains.

A is iframed on page B.

I need to call a function which is on page B from page A.

Is this possible without something like porthole?

Basically what I'm trying to do is a simple push notification (A notifies B), don't need to transfer any content.


There is an HTML5 way to do this with cross window messaging.

There are various work-arounds when HTML5 isn't available that can be used even between different origins.


If you want to avoid using that specific library you could use the concept it's based off of and roll your own. Here's a pretty in depth article that describes the technique without the use of a library: http://softwareas.com/cross-domain-communication-with-iframes.


Put A and B with the same domain, different subdomain is fine. Then in the iframe A, you can call parent.function_name (parameter1, parameter 2). If you need to call that function very often, you may want to do this :

function p(data, div_id){parent.p(data, div_id);}  

then in iframe A, you can just call p(data, div_id), which will actually call page B's function. I have used this a lot of comet streaming. If you are interested, you may read this, which basically use iframe to communicate with data:

http://www.shanison.com/2010/05/10/stop-the-browser-“throbber-of-doom”-while-loading-comet-forever-iframe/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号