If you’re trying to set focus to an element in an iframe
that has already been loaded, you need to set focus to the iframe
first, then to the element.
Here is sample code:
if (ie) {
var myIframe = document.frames['my_iframe'];
var myField = document.frames['my_iframe'].document.getElementById('my_field');
}
else {
var myIframe = document.getElementById('my_iframe');
var myField = document.getElementById('my_iframe').contentDocument.getElementById('my_field');
}
if (!safari) {
myIframe.focus();
}
myField.focus();
In my use case, Safari didn’t take the iframe
focus properly, but I didn’t take the time to test a generic case. If someone has time or interest in testing, please add a comment with your findings.
Hopefully this prevents someone from wasting as much time on this as I did.
How would I go about doing the opposite of that – selecting an element in the page that the iframe is embedded in from the iframe?
Is that even possible?
“top.” will always give you the topmost window. You can also use “parent.” and perhaps “opener.” (not sure how it works with iframes).
opener will do fine with iframe.
opener will point to the opening window. From there you should be able to descend to it’s document.