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

156
Views
beforeunload is navigating to previously visited page instead of POST action url on form submit in Javascript
  1. I have a page where I want to configure dates by selecting dates with date picker and add to dynamic table using javascript in same page.
  2. I have multiple dates in the table but not saved or not clicked submit button yet but clicked on sub tab in same page to navigate to another page and a popup opened saying you have unsaved changes
  3. Clicked on stay on same page and the added dates are present on the table and clicked on submit button.

Why instead of submitting the form, it redirects to previously visited tab(sub tab mentioned in step 2)?

Below code is to warn, if there is unsaved data:

var submitted = false;
     
$(document).ready(function() {
  $('#addDates').click( function() {
    submitted=true;
  });
     
  window.onbeforeunload = function () {
    if (!submitted) {
      return 'Do you really want to leave the page?';
    } else {
      window.onbeforeunload = null;
    }
  }
});

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

0

maybe try to overwrite the event with an empty function:

var submitted = false;

$(document).ready(function() {
   $('#addDates').click( function() {
    submitted=true;
});

window.onbeforeunload = function () {
    if (!submitted) {
      return 'Do you really want to leave the page?';
    } else{
        window.onbeforeunload = function() {};
    }
  }
});
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!