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

93
Views
Easly catch page reload in JavaScript

I need to do something when the browser is closing, so first I need to know that it closes. What I found was:

<script>
var closing = true;
$(function () {
  $("a,button[type=submit]").click(function()
    {
      closing=false;
  });
  $(window).on("unload",function(){
      if (closing){...}
    });
  });
</script>

It works good/nice/etc but there is one "catch": page reload... When the page is reloaded, it's not any click, so it's treated as "closing"... Is there such an easy way to catch the reload (as for the "click" written above)?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Try to use navigation object property, there you can find the information page is reloaded(value is equal to 1) or not:

<script>
var closing = true;
var performance = window.performance;
$(function () {
  if((performance.navigation && performance.navigation.type === 1) ||
    performance
      .getEntriesByType('navigation')
      .map((nav) => nav.type)
      .includes('reload')){
        closing = false;
        console.log("reloaded");
  }
  $("a,button[type=submit]").click(function()
    {
      closing = false;
  });
  $(window).on("unload",function(){
      if (closing){
       console.log("closing");
      }
  });
});
</script>
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!