Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

124
Vistas
A text is displayed only right after my javascript is triggered

I wrote javascript codes.

By clicking the button, the child window pops up and displays a text sent from the parent window using a postMessage function.

My code could sent a text to the child window, but there's no text displayed. The text is displayed only when I keep clicking the button. I don't want the text to disappear.

I think my code is overridden by a blank script or something, though I don't write any other codes except for below.

Do you have any solution for this?

the parent window html

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Parent Window</title>
</head>
<body>
  <input type="button" value="TEST_BUTTON" id="testButton">

  <script>
      var testButton = document.getElementById('testButton');
      testButton.addEventListener('click', function(event) {
        event.preventDefault();
        var newWindow = window.open('./child_window.html', 'popupWindow', 'width=400,height=300');
        newWindow.postMessage('this is a content from the parent window.', '*');
        return false;
      },false);
  </script>
</body>
</html>

the child window html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Pop Up Window</title>
  </head>
  <body>
    <h1 id="mainText"></h1>
    <script type="text/javascript">
      var mainText = document.getElementById('mainText');

      window.addEventListener('message', function(event) {
        console.log(event.data);
        this.mainText.innerText = event.data;
      }, false)
    </script>
  </body>
</html>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I ended this up using localStorage instead.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda