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

104
Visualizações
document.querySelector works in console but not on realtime

I'm creating a simple html page with one audio player inside an iframe. I need to enamble kind of autoplay for desktop and mobile.

The player is this one:

<div style="width: 100%"><iframe src="https://players.rcast.net/fixedbar1/66549" frameborder="0" scrolling="no" autoplay style="width: 100%"></iframe></div> 

I put this block on the bottom of the html page:

<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script>

    
    
    document.addEventListener("DOMContentLoaded", function() { 
        // this function runs when the DOM is ready
        setTimeout(function(){
            document.querySelector('.play-btn').click();
        },3000); //delay is in milliseconds 

    });

</script>

Using firefox console document.querySelector('.play-btn').click(); works fine, but on runtime i get:

Uncaught TypeError: document.querySelector(...) is null

Any ideas or best ways?

Thanks, Red

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

0

You can do whatever you want in the console but you would only be able to access iframe content programmatically if your iframe domain matches your current domain.

That being said, you can:

Select the iframe, then run query selector on the iframe (you don't even need jQuery for it):

const iframeElement = document.querySelector('iframe'); 
iframeElement.querySelector('.play-btn').click();

Tips:

  1. You can also play the video/music directly by calling play() on the media elements. So you can cut out stimulating click on the button.

  2. querySelector is slower than getElementById, you can assign an id attribute to your iframe/button/media element and find it directly.

    It will also help you avoid bugs because querySelector returns the first match. So in case you have multiple iframe or multiple elements with the class .play-btn, it can lead to unexpected behaviour.

about 4 years ago · Juan Pablo Isaza Relatório

0

you should select iframe at first and get its content window to access all elements on it.

const myIframe = document.querySelector('#iframe_id')
const myIframeDocument =myIframe.contentWindow.document
const myElement = myIframeDocument.body.querySelector('#target_element')

it is noticeable that the iframe should be loaded completely before your process and also domain conflict

about 4 years ago · Juan Pablo Isaza Relatório

0

Hope this helps:

   <iframe id="video1" width="450" height="280" src="http://www.youtube.com/embed/TJ2X4dFhAC0?enablejsapi" frameborder="0" allowtransparency="true" allowfullscreen></iframe>
<a href="#" id="playvideo">Play button</a>
<script>
 $("#playvideo").click(function(){
  $("#video1")[0].src += "?autoplay=1";
 });
</script>

I found this on Grepper, but it was from another Stack Overflow post. If that doesn't help I found a different post that seems to be more related to the error.

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