Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

181
Visualizações
Recursively locate element within nested iframes

My goal is to select an element regardless of where it is located inside a chain of nested iframes.

I am basically looking for a vanilla javascript equivalent of this: https://stackoverflow.com/a/11224815/1737287

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think you will not be able to search for an iframe within an iframe without changing the document's "context" using something like

iframe.contentDocument

method. You can read more about that here

In your case, you need to grab all of the iframes recursively as you reset the contentDocument as needed. Something similar to this may work for your purposes

function getAllIframes() {
            let allIframes = [];
            let topLevelIframes  = document.getElementsByTagName('iframe');
            for (let i = 0; i < topLevelIframes.length;i++) {
                    console.log(topLevelIframes[i])
                    traverseDeeperIntoIframes(topLevelIframes[i])
            }
            function traverseDeeperIntoIframes(ctx) {
                allIframes.push(ctx)
                // Fallback for contentWindow
                const iframeContext = (ctx.contentDocument) ? ctx.contentDocument : ctx.contentWindow.document
                const tmpFrames = iframeContext.getElementsByTagName('iframe')
                console.log(iframeContext, tmpFrames)

                for (let i = 0; i < tmpFrames.length;i++) {
                    traverseDeeperIntoIframes(tmpFrames[i])
                }
            }
            return allIframes;
} 

        const allIframes = getAllIframes()

        // Iterate over this allIframes <Array> resetting the contentWindow at every context.
        // For example 
        function queryOverAllContentWindows(allIframes, cb) {
            for (let i = 0; i < allIframes.length;i++) {
                const ctx = (allIframes[i].contentDocument) ? allIframes[i].contentDocument : allIframes[i].contentWindow.document

                // Pass the iframe context into any arbitrary callback
                cb(ctx)
            }
        }
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda