javascript - About iframes and event listeners - Stack Overflow

Consider a page that contains an iframe. The iframe source is on another domain.I'd be interested

Consider a page that contains an iframe. The iframe source is on another domain.

I'd be interested in 2 things:

  1. Can I create an event listener inside the iframe that listens to events that take place in the parent window (and runs a function in the iframe)?

  2. Can I create an event listener inside the parent window that listens to events that take place in the iframe (and runs a function in the parent window)?

Consider a page that contains an iframe. The iframe source is on another domain.

I'd be interested in 2 things:

  1. Can I create an event listener inside the iframe that listens to events that take place in the parent window (and runs a function in the iframe)?

  2. Can I create an event listener inside the parent window that listens to events that take place in the iframe (and runs a function in the parent window)?

Share Improve this question edited Jun 28, 2020 at 15:37 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked May 25, 2011 at 11:48 zozozozo 8,61219 gold badges83 silver badges141 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 3

That's a no on both accounts, cross-domain scripting security measures disallow any cross domain munication.

You might want to take a look at this indepth article about Cross-Domain Communication with IFrames. It talks about what you can and can't do, and provides some alternatives like window.postMessage

Yes. But it might be more work than you're interested in. You'll need to create a proxy for that page that lives on the same domain as the parent page. Your proxy could be a simple pass-through, but it could very well break the page; some URL-rewriting might be required in order to not break scripts that run on that page that assume things about the context it's being evaluated in, so on that front "it depends."

But your question is "Can you ...?" and the answer is "Yes."

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742327154a4422976.html

相关推荐

  • javascript - About iframes and event listeners - Stack Overflow

    Consider a page that contains an iframe. The iframe source is on another domain.I'd be interested

    9小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信