3

So I have an iFrame that contains a form. I would like for the page to focus in on the input element "foo" when the page loads. Is there a way to do this?

kelunik
  • 6,520
  • 2
  • 38
  • 69

1 Answers1

2

if your iframe has src attribute with the same domain as the parent document then it is simple:

<iframe src=" ... " onload="document.getElementById('foo').focus();"></iframe>

but if not, then you have a problem with 'same origin policy':

1. if you have access to modify document inside iframe (I mean if you can add some javascript code there) it's a bit tricky but possible (here's a good article about that: http://softwareas.com/cross-domain-communication-with-iframes)
2. if you have a 3-rd party website inside your iframe then it is imposible
(the only way is to use some dns+document.domain tricks but they are extremely ugly)

tsds
  • 8,002
  • 9
  • 58
  • 82