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

105
Views
Javascript: Modern working approach to refresh the page redirects to another location

I have tried some answers on SO, but they are old and no longer work, or are blocked by the browser.

I have a login, and a separate login failed page (separate for a good reason, please don't say make them the same page).

If the user is shown the failed login page, and they hit refresh, I would like to go to the login page, not refresh the failed login page.

Existing answers go something like this:

<script type="module">
    function redirect()
    {
        window.location.href="/login/";
        return true;
    }
    window.addEventListener('beforeunload', redirect);
</script>
about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

Not ideal, but instead of catching the reload before navigating away, catch the reload at the start of the page:

Reload detection code from: https://stackoverflow.com/a/53307588/5506400

<script type="module">
    const pageAccessedByReload = (
    (window.performance.navigation && window.performance.navigation.type === 1) ||
        window.performance
        .getEntriesByType('navigation')
        .map((nav) => nav.type)
        .includes('reload')
    );
    if (pageAccessedByReload) {
        window.location.href="/login/";
    }
</script>
about 4 years ago · Santiago Gelvez Report

0

let me know this will help or not, use type value to check for reload

  // Use getEntriesByType() to just get the "navigation" events
  var entries = performance.getEntriesByType('navigation');

  for (var i = 0; i < entries.length; i++) {
    console.log('= Navigation entry[' + i + ']');
    var p = entries[i];
    // dom Properties
    console.log(
      'DOM content loaded = ' +
        (p.domContentLoadedEventEnd - p.domContentLoadedEventStart)
    );
    console.log('DOM complete = ' + p.domComplete);

    // other properties
    console.log('type = ' + p.type);
  }

about 4 years ago · Santiago Gelvez 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!