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

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

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 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!