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

239
Vistas
How to display a counter in a window and make it close?

I have this code in which when clicking a button a window opens and a counter appears that is a random number between 1 and 100, this counts up to 0 and the window closes, but I don't know very well what I'm doing wrong since nothing appears in the window that I open. Here I leave part of the html code because I decide to open it by giving a button and the javascript.

var ventana;

function abrir() {
  ventana = window.open("", "new_window", "width=800,height=500,status=no,toolbar=no,menubar=no");
}

window.onload = contar;

function contar() {
  var contartiempo = Math.floor(Math.random() * 100);
  ventana.document.getElementById('contador').innerHTML = contartiempo;
  if (contartiempo == 0) {
    ventana.close();
  }
  ventana.document.write(" tu contador:<span id = contador></span>") //quiero que se muestre en la ventana abierta el contador
}
<body>
  <input type="submit" value="Abrir" onclick="abrir()">
</body>

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I have already solved my problem, the code shows a window, performs the counter and closes that window, here the result

var ventana;

function abrir() {
  ventana = window.open("", "new_window", "width=800,height=500,status=no,toolbar=no,menubar=no");

  contar()
}
window.onload = contar//para que cuando 
var contartiempo = Math.floor(Math.random() * 100);
function contar() {
  if (ventana) {

    ventana.document.write(" <span id = contador></span>") //quiero que se muestre en la ventana abierta el contador
    ventana.document.getElementById('contador').innerHTML = contartiempo;
    if (contartiempo == 0) {
      ventana.close();
    } else{
        contartiempo-=1;
        setTimeout("contar()",1000);
    }
    
  }
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

So, there are a lot of concern in this code:

  1. You're loading contar function on load and till that time ventana is undefined hence you'll get an error when you do something like ventana.whatever (trying to access a property on undefined).

  2. Since the document in the new page has no span with id = contador when created, the line ventana.document.getElementById('contador').innerHTML = contartiempo; will also fail.

To fix it:

var ventana;

function abrir() {
  ventana = window.open("", "new_window", "width=800,height=500,status=no,toolbar=no,menubar=no");

  contar()
}

function contar() {
  if (ventana) {
    var contartiempo = Math.floor(Math.random() * 100);
    ventana.document.write(" tu contador:<span id = contador></span>") //quiero que se muestre en la ventana abierta el contador
    ventana.document.getElementById('contador').innerHTML = contartiempo;
    if (contartiempo == 0) {
      ventana.close();
    }
    
  }
}

Here I am calling contar function on click of the button in parent document after we have opened the new window. Then I am putting the span & the other layout on the page with ventana.document.write. Then we are adding an innerHTML to that span.

HTML part will require no changes.

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