Jeevan
Jeevan

Reputation: 3938

How to set focus to top frame within an iframe crossdomain.

I have an iframe in my web-page. So when Esc is pressed inside the iframe I need to set focus the top frame of document. The iframe is from an different domain. enter image description here

Here Frame-B URL will be some abc.com and frame-A will be from xyz.com

So when an event is fired in Frame-B I need to focus Frame-A.

I can bind any number of events in Frame-A and Frame-B.

I have found a old way like this "An iframe in an iframe in an iframe". which am not interested in.

Any Idea how this can be done?

Upvotes: 0

Views: 1939

Answers (1)

Jakob
Jakob

Reputation: 24370

If you have some control over the outer frame (let's say it's your site, but still a different one than B), then there's a number of solutions to your problem. If you don't have any control over the outer frame, (because it is google.com for example) then there's nothing you can do. The browsers are designed to prevent it. See Jonas answer.

So what about if you can control the outer page? Any of the following would work:

  • Use postMessage. All you have to do is adding an very generic event listener in Frame-A. It's available in "all" browsers except IE7 and below.
  • Use a server as a middleman. Send a request from Frame-B to your server and let Frame-A access it from there. If you don't want to poll, you could use web sockets in Frame-A (or rather a good web socket lib, that fallbacks on other technologies if web sockets are not present).
  • Use the fragment identifier hack. You can read about that (and several other techniques) here: http://softwareas.com/cross-domain-communication-with-iframes

Upvotes: 4

Related Questions