How to pass focus from an iframe to next focusable element in host page?

2.8k views Asked by At

Background

I have an iframe widget which will be embedded in other pages - I have no prior knowledge of the host page's structure and code, and the iframe will be cross-domain and likely sandboxed.

My goal is to give the iframe widget a single tab stop - after focusing on it there's a dedicated key combination to "enter" the widget, but normal tabbing should skip forward to the next focusable element in the host page.

Question: After the iframe receives focus and a new tab keypress is detected, how can I pass the focus to the host page's next focusable element?

Simple example:

<!-- host page -->
<button> first button </button>
<iframe src="myIframeWidget.com"/>
<button> second button </button>

In the above, if the first button is focused, then the expected behavior is that on first tab the iframe will receive focus, and on the next tab the second button will receive focus (skipping all focusable elements inside the iframe).

Once the iframe is focused and tab is pressed, I need to pass the focus from my iframe to the second button.

Notes

  1. Since my iframe DOM is rather large, I don't want to set tabindex=-1 to all internal focusable elements.
  2. At the time of writing, aria-hidden="true" does not remove an element or its descendants from the focus sequence. I'm not aware of any other aria attribute which removes an entire subtree from the focus sequence.
  3. A pure JS solution is preferred to one relaying on external packages.
  4. I'll also need to handle reverse-tabbing, but I assume that the same approach for forward-tabbing will apply - if there are any crucial differences, please mention them in your answer if possible.
  5. There's an edge case where there isn't anything focusable after the widget - typically in those cases the browser will focus on the address bar buttons - if there's a way to handle that it'll be great to know, but this probably should be handled in a separate question.

Thanks!

3

There are 3 answers

4
Khokon M. On BEST ANSWER

Let's say, this is the content of the host page

<button>Main button 1</button>
<button>Main button 2</button>
<iframe id="frame1" name="frame1" src="iframe.html"></iframe>
<button>Main Button 3</button>

Now, when you receive the focus on the iframe, which is iframe.html, on the next tab press, you want to pass the focus to the Main Button 3 button. You might have a lot of focusable content on your iframe.html like,

<button>I frame button 1</button>
<button>I frame button 2</button>

that you want to skip. To do that, you can write a simple javascript inside your iframe.html Which is,

let go_next = false;
document.body.addEventListener('keyup', (e)=> {
    if( e.which == 9 ) {
        if(go_next){
            window.parent.document.getElementById('frame1').nextElementSibling.focus()
            go_next=false
        }else{
            go_next =true;
        }
    }
});

Code Explanation

We don't want the focus to immediately pass to the next element, as the user should be able to focus on the iframe as well. For that reason, the script is assuming that the user has focused on the iframe for the first time by declaring variable go_next = false

Note: Focusing from Main Button 2 to iframe is counted as a tab press inside the iframe.html. That's why we have to ignore the first tab press inside the iframe.html

So, just after the user has focused on the iframe, we're making our variable go_next = true. Now on the next tab press, we can proceed to pass the focus to the next element of the host page.

To do that, we are grabbing the host page using window.parent and selecting the iframe from the host page using document.getElementById('frame1') and the next element of the iframe using nextElementSibling and focusing the next element using the focus function. And after passing the focus, we're making the go_next = false again for the next time.

I've prepared a small demonstration for you to test here

Alternative Solution For Cross Domains

Cross domains often block access to the parent host using Content Script Policy. To overcome this issue, we have to play a bit tricky. We're are going to create an invisible button at the end of our iframe.html page. Eg:

<button id="last_element" style="opacity:0;filter:alpha(opacity=0);">Hello</button>

Remember we said the tab press that was used to focus the iframe can be accessed inside the iframe.html? Well, we're going to take that advantage and focus on the last button of our iframe.html. To do that, add this script at the end of the iframe.html

document.body.addEventListener('keyup', (e)=> {
    if( e.which == 9 ) {
        document.querySelector("#last_element").focus()
    }
});

Now, as the last element of the iframe is focused, the next focus will be always the next element of the host page. Here's a demo on codepen

0
Diego On

There is a much simpler solution. Search for the last element inside the iframe, and focus it without using the prevent default. Not using prevent default will cause it to focus on the next focusable element.

// event.preventDefault(); 
lastFocusableElement.focus();
0
maetz007 On

This approach of moving focus around iframe content essentially traps keyboard-only users on one side of the iframe which results in a WCAG violation (2.1.1 and 2.1.2). From an accessibility point of view, this method should be avoided.

If you want to provide users with the means to bypass an iframe, ideally you would provide "skip" links before and after the iframe that allows users to skip the iframe content. You want to "bookend" the iframe with skip links so keyboard-only users can use TAB or SHIFT-TAB navigation.