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

246
Visualizações
window.postMessage to popups in Angular

What I want to do: opening an popup and send the postMessage when the popup is ready.

Problem: I ran into Race Condition which the popup is not ready but the message is sent. I tried to listen to the "INIT" message and in the popup send back message. But the problem is when network latency or some slow computer will not receive the initial message.

The setTimeout obviously not a good solution

Code I have problem with: Parent Window

    const windowFeatures  = "height=800,width=1280,toolbar=1,menubar=1,location=1"; 
    const printWindow = window.open("/print", "Print_Me", windowFeatures);

    setTimeout(() => {printWindow.postMessage({printPageStatus: "INIT"}, window.origin)}, 1000)

    window.addEventListener("message", (event) => { 
      if(event.origin !== window.origin)  return;  
      if(event.data.printPageStatus === "READY") {
        printWindow.postMessage({message: "from parent", window.origin);
        return;
      }
    });

The popup window

 constructor() {
    window.addEventListener("message", event => {
      if(event.origin !== window.origin)  return;    
      if(event.data.printPageStatus === "INIT")
        this.sendReadyConfirmation(event);
      if(event.data.message === "from parent") {
        this.processMessages(event.data);
      }
    }, false);
 }

  sendReadyConfirmation(e): void {
    e.source.postMessage({printPageStatus: "READY"}, e.origin);
  }

Thank you

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

0

What you need to do is send the message when the window has loaded successfully :

const printWindow = window.open("/print", "Print_Me", windowFeatures);
 printWindow.onload = () => {
   printWindow.postMessage({printPageStatus: "INIT"}, window.origin)
};
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