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

187
Vistas
Should setting window.location.href cause the "old" page to stop running JavaScript?

I'm seeing unexpected behavior when setting window.location.href. My understanding is that the current page will be navigated away from immediately -- effectively ignoring subsequent JavaScript in the containing script. However, this is not what I'm seeing in practice (Firefox, Chrome and mobile Safari). I'm setting window.location.href when I encounter an error condition (e.g. missing some data) and yet the script continues to run and spew a bunch of errors because of said error condition. (This also applies to window.location.assign.)

Example:

function handleError() {
    window.location.href = "https://example.com"
}

function doWork(id) {
    if (!id) {
        handleError();
    }
    var oops = id.split("-");
    // a bunch of errors spill into the console, onerror listeners, etc.
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Why not simply add a return after the guard clause?

function handleError() {
    window.location.href = "https://example.com"
}

function doWork(id) {
    if (!id) {
        handleError();
        return 
    }
    var oops = id.split("-");
    // a bunch of errors spill into the console, onerror listeners, etc.
}
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