Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
Manejar el enfoque de tabulación con iFrame de origen cruzado dentro de la trampa modal

Estoy tratando de resolver un problema de tabulación dentro de mi modal cuando está presente un <iframe> de origen cruzado.

Lo que estoy notando:

  1. Si el iFrame no es el primer o último elemento enfocable en el modal, no es necesario configurar .focus() con javascript.
  2. Si el iFrame es el primer o último elemento enfocable en el modal, necesito configurar .focus() con javascript para mantener la accesibilidad del teclado.

.focus() está restringido en este uso. Y como no puedo establecer el enfoque, la trampa de tabulación del teclado modal está rota.

¿Alguien ha tenido éxito en esta área?

Ejemplos HTML :

Funciona (no necesito establecer el foco con javascript en el iframe)

 <input type="text" /> <iframe /> <input type="text" />

Si iframe es el primer o último elemento enfocable, tendré que usar javascript para establecer el enfoque para mantener la trampa del teclado modal

 <input type="text" /> <iframe />

o

 <iframe /> <input type="text" />

JavaScript :

 var KEY = { TAB: 9, }, allowedFocusableElements = 'a[href], area[href], input:not([disabled]):not([type="hidden"]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), iframe, object, embed, *[tabindex], *[contenteditable]'; function openDialog(e) { // Get focusable elements. dialogFocusableElements = dialog.querySelectorAll(allowedFocusableElements); dialogFirstElement = dialogFocusableElements[0]; dialogLastElement = dialogFocusableElements[dialogFocusableElements.length - 1]; document.addEventListener("keydown", processKeys); } function processKeys(event) { if (event.keyCode === KEY.TAB && !event.shiftKey) { if (document.activeElement === dialogLastElement) { event.preventDefault(); dialogFirstElement.focus(); } } else if (event.keyCode === KEY.TAB && event.shiftKey) { if (document.activeElement === dialogFirstElement) { event.preventDefault(); dialogLastElement.focus(); } } }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Todo esto se redujo a tratar de usar .focus() en un iFrame de origen cruzado. Dado que no parece haber ninguna forma de evitar esto (considerando el control del orden de las pestañas y el enfoque con javascript), he implementado (no tan idealmente) algunos elementos de pestañas adicionales antes o después del iFrame dependiendo de dónde se encuentre en el modal.

Si el iFrame es el primer elemento enfocable, colocamos un elemento "pseudo" tabulado antes de él y, de la misma manera, si es el último elemento enfocable, colocamos un elemento tabulado después.

Aquí está ese fragmento de código en particular:

 var KEY = { TAB: 9, }, allowedFocusableElements = 'a[href], area[href], input:not([disabled]):not([type="hidden"]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), iframe, object, embed, *[tabindex], *[contenteditable]'; function openDialog(e) { // Get focusable elements. dialogFocusableElements = dialog.querySelectorAll(allowedFocusableElements); dialogFirstElement = dialogFocusableElements[0]; dialogLastElement = dialogFocusableElements[dialogFocusableElements.length - 1]; // Tab helpers for iFrames. if (dialogFirstElement.tagName && dialogFirstElement.tagName === "IFRAME") { iframeTabHelper = document.createElement("div"); iframeTabHelper.setAttribute("tabindex", "0"); iframeTabHelper.classList.add("iframe-helper-button"); dialogFirstElement.parentNode.insertBefore(iframeTabHelper, dialogFirstElement); // Re-get focusable elements. dialogFocusableElements = dialog.querySelectorAll( allowedFocusableElements ); dialogFirstElement = dialogFocusableElements[0]; } if (dialogLastElement.tagName && dialogLastElement.tagName === "IFRAME") { iframeTabHelper = document.createElement("div"); iframeTabHelper.setAttribute("tabindex", "0"); iframeTabHelper.classList.add("iframe-helper-button"); dialogLastElement.parentNode.insertBefore(iframeTabHelper, dialogLastElement.nextSibling); // Re-get focusable elements. dialogFocusableElements = dialog.querySelectorAll( allowedFocusableElements ); dialogLastElement = dialogFocusableElements[dialogFocusableElements.length - 1]; } document.addEventListener("keydown", processKeys); } function processKeys(event) { if (event.keyCode === KEY.TAB && !event.shiftKey) { if (document.activeElement === dialogLastElement) { event.preventDefault(); dialogFirstElement.focus(); } } else if (event.keyCode === KEY.TAB && event.shiftKey) { if (document.activeElement === dialogFirstElement) { event.preventDefault(); dialogLastElement.focus(); } } }

No es perfecto, pero mantiene el funcionamiento de las pestañas y la trampa del teclado.

Probablemente podría agregar algunas comprobaciones para verificar si el iFrame es de origen cruzado, o incluso mejor, reescribir para verificar si hay algún elemento que restrinja la aplicación .focus() , no solo iFrames.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!