Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

236
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!