How to focus on IFrame

I have an IFrame with some buttons in it. When buttons are clicked, the parent DOM is processed (all in the same domain, so don't worry about policies of the same origin).

Since the user clicked the button, the focus changes to the IFrame. However, I would like to move the focus back to the parent document (mainly due to this FireFox problem ).

How can i do this?

Edit: I created js-fiddle that shows the problem. While it focus()works, if you call it on the elements of the parent form, it does not work at all. As you can see from the js script, there are no form elements in my parent document.

+5
source share
2

iframe, , .

:

outer.html

<html>
  <head>
    <title>Outer</title>
  </head>
  <body>
    <iframe id="iframe" src="inner.html">
    </iframe>
  </body>
</html>

inner.html

<html>
  <head>
    <title>Inner</title>
  </head>
  <body>
    <form id="innerForm">
      <input type="button" id="innerButton" value="Inner button" />
    </form>
  </body>
  <script type="text/javascript">
    // When we click the inner button...
    document.getElementById('innerButton').addEventListener('click', function() {
      /*
       * Do whatever you need to do here
       */

      // Focus the parent
      parent.focus();
    });
  </script>
</html>
+2

- , IFrame:

document.getElementById("myElementId").focus(); 

IFrame.

0

All Articles