简体   繁体   English

来自外部域iframe的JavaScript调用功能

[英]Javascript call function from an external domain iframe

Page A and page B are on different domains. 页面A和页面B在不同的域上。

A is iframed on page B. A在页面B上格式化。

I need to call a function which is on page B from page A. 我需要从A页调用B页上的函数。

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. 基本上我想做的是一个简单的推送通知(A通知B),不需要传输任何内容。

There is an HTML5 way to do this with cross window messaging . 有一种HTML5方式可用于跨窗口消息传递

There are various work-arounds when HTML5 isn't available that can be used even between different origins. 当HTML5不可用时,有多种解决方法,即使在不同来源之间也可以使用。

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 . 这是一篇非常深入的文章,描述了不使用库的技术: http : //softwareas.com/cross-domain-communication-with-iframes

Put A and B with the same domain, different subdomain is fine. 将A和B放在相同的域中,可以使用不同的子域。 Then in the iframe A, you can call parent.function_name (parameter1, parameter 2). 然后,在iframe A中,您可以调用parent.function_name(参数1,参数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. 那么在iframe A中,您只需调用p(data,div_id),它实际上就会调用页面B的函数。 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: 如果您有兴趣,可以阅读以下内容,该内容基本上使用iframe与数据进行通信:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM