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

176
Vistas
How can I handle Uncaught TypeError of a variable in JavaScript?

I'm trying to open a tab multiple times using the JavaScript code given below..

function webAssign() {
  let i;
  let myWin;
  for (i = 1; i < 4; i++) {
    console.log(i);
      
    myWin = window.open("index.html");
    myWin.close();
  }
}

After running this code, it throws the error given below..

Uncaught TypeError: myWin is null

How can I solve this problem?

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

0

From MDN's page for window.open:

Return value

A WindowProxy object, which is basically a thin wrapper for the Window object representing the newly created window, and has all its features available. If the window couldn't be opened, the returned value is instead null.

(my emphasis)

There are contexts (environments where your code may be running) that can't open windows, or can't open windows to certain origins. In those contexts, window.open returns null. For example, the Stack Snippets feature here on SO disallows window.open:

function webAssign() {
  let i;
  let myWin;
  for (i = 1; i < 4; i++) {
    console.log(i);
      
    myWin = window.open("index.html");
    myWin.close();
  }
}
webAssign();

Running that on Chrome gives the error (in the web console):

Blocked opening 'https://stacksnippets.net/index.html' in a new window because the request was made in a sandboxed frame whose 'allow-popups' permission is not set.


But separately from that, opening and immediately closing windows in a tight loop doesn't really make any sense.

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