/    Sign up×
Community /Pin to ProfileBookmark

Iframe Onfocus

I have a very peculiar case which happens only with mozilla firefox and not with IE.

The onfocus event is not working if I try to set for an IFRAME.

<iframe [B]onfocus=”setFocus_(this)”[/B] style=”display:none;” width=”0″ height=”0″ frameborder=”0″ scrolling=”no” align=”center” ID=”frame1″ ALLOWTRANSPARENCY=”true” ></iframe>

The setFocus_() method is not invoked if I try to focus on the iframe. This works very well in IE.

Please help me ?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@toicontienDec 06.2006 — I ran into this problem earlier when writing a form validation routine. Gecko browsers (Firefox, Netscape 7+, Mozilla) do not allow you to set focus to the same element when its focus event fires. I think it was their hope to avoid infinite loops where an element repeatedly focuses itself.
Copy linkTweet thisAlerts:
@drahulauthorDec 07.2006 — I posted my problem y'day night and now its in the third page..... this show how active this forum is !!!

so thought, just say hi to all and make it appear above rest of the postings.


Rgs,

Rahul

?
Copy linkTweet thisAlerts:
@toicontienDec 07.2006 — Actually, I posted the answer to your question already ? Firefox won't allow the iframe to set focus to itself in the onfocus event. Firefox won't let any element set focus to itself in the onfocus event. That's just the way it is. I did discover one workaround though.

<i>
</i>function getFocusFunc(objId) {
function doFocus() {
if (document.getElementById) {
document.getElementById(objId).focus();
}
}
return doFocus;
}

function set_focus() {
// Do whatever until you get to the point where you need to set focus to the iframe
setTimeout(getFocusFunc('myIFRAME'), 10);
}

The setTimeout() function executes the function returned from the getFocusFunc() function (kind of confusing I know ? ). getFocusFunc() returns a function object, that when executed at the end of setTimeout(), is executed in the scope of the window object, instead of the onfocus event object of the iframe. This gets around Firefox's blocking of circular events.
×

Success!

Help @drahul spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...