Page 1 of 1

[solved] Focusing on an iFrame without hitting anything.

Posted: Sun Jan 03, 2010 11:05 am
by Vael Victus
I have a page that has an iframe on it, and I let the user use the keyboard to select various movement styles. (right arrow key moves their character to the right) This great, but they have to actually focus on the iFrame by clicking on something. I've seen javascript that will focus on the iFrame but you need to click a link to actually focus in.

In summary, the problem is that they will be taken to another page at times, then will come back, but they can't move with the arrow keys until they click random white space in the iframe itself.

Re: Focusing on an iFrame without hitting anything.

Posted: Sun Jan 03, 2010 1:38 pm
by AbraCadaver
Probably several ways to do it. Here is one that might work. You can put it at the end of the page or in a function in the head and use an onload() in the body tag to call it.

Code: Select all

var fr = document.getElementById("myIframeID");
 
if (document.all) //IE
    fr.document.body.focus();
else //Firefox
    fr.contentDocument.body.focus();

Re: Focusing on an iFrame without hitting anything.

Posted: Tue Jan 05, 2010 9:44 am
by Vael Victus
Perfect, thank you so much!