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?
Asked
Active
Viewed 2,813 times
3
-
Is that what you are after? http://stackoverflow.com/questions/369026/setting-focus-to-iframe-contents – Pav May 06 '11 at 01:46
-
no, im looking to focus on an element INSIDE the iframe, not the iframe itself – May 06 '11 at 01:50
-
1in the example above, you can use element id/name instead of `contentWindow` – Pav May 06 '11 at 01:54
-
:O really? thanks! will try now! – May 06 '11 at 02:00
1 Answers
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