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

133
Visualizações
communicating between my page and iframe and sending data to each other

I'm trying to communitacting between my page and another iframe that is another page, which I have,

On my iframe page, I inserted this script on the head

if(typeof window !== 'undefined'){
 window.addEventListener('message', event => {
    if (event.origin.startsWith('https://testpage.com/')) { 
        document.querySelector('.home-title').classList.add('test');
        event.source.postMessage('iframe', event.origin);
    } else {
        
        return; 
    } 
 }); 
}

and on my main page where I want to communicate with my iframe, I have this code,

<script>
  window.onload = function () {
  setTimeout(() => {
     const iframe = document.querySelector('iframe');
     const iframeWindow =  iframe.contentWindow;
     iframeWindow.postMessage("Hello there" , 'https://testIfame.com/');
     alert('here alert pops-u');
  }, 3000)
 

 window.addEventListener('message', function(event) {
     if (event.origin == 'https://testpage.com/'){
       alert('asd');
      } 
    }, false);
  }
</script>

The first alert, works, it pops up, but nothing else. Can somebody help me to resolve this. Thanks a lot.

about 4 years ago · Juan Pablo Isaza
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