Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9244983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:09:44+00:00 2026-06-18T09:09:44+00:00

I use onbeforeunload to reset a variable with Ajax request. But it is executed

  • 0

I use onbeforeunload to reset a variable with Ajax request. But it is executed once only. E.g if I visit the page (after log in) and close the window, the function is executed, but if I repeat the operation (putting the address in the browser with login done and closing the window) the function isn’t executed.

window.onbeforeunload = function (e) {
    //Ajax request to reset variable in database 
    resetDemoValueWithAjax();
};

//Ajax request to reset variable in database and exit
function resetDemoValueWithAjax(){
    $.ajax({
        async:true,
        type: "POST",
        dataType: "json",  
        url:"http://localhost/site/index.php/welcome/resetDemoValue",
        data: {name: "demo"},
        success:function(){
            document.location.href='http://localhost/site/index.php/auth/logout';   
        },
        error: function(){
            document.location.href='http://localhost/site/index.php/auth/logout';   
        } 
    });     
}

document.location.href='http://localhost/site/index.php/auth/logout'; Only is used in another moment: When the user does logout. Not here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:09:46+00:00Added an answer on June 18, 2026 at 9:09 am

    You have a race condition. The race is between the asynchronous Ajax request and the browser’s attempt to navigate away from the page (which is what caused beforeunload in the first place). The navigation away will probably always win, so the browser moves away from the page before the Ajax callback can complete.

    If you made the Ajax request synchronous with async:false, this race wouldn’t happen, since the JavaScript thread would block until the Ajax request resolved. Be aware that this makes for a potentially annoying user experience (i.e., the user tries to close the page but has to wait for the Ajax request to resolve before the tab will close).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I use javascript to save information after page going to be close or
Use of gradient images is very common among developers for styling a page. Gradient
Im not sure that it is the right way but I use this functions
We use jQuery's global ajaxError() handler to alert the user of any AJAX failures:
I have a page call A Asp.Net and i want to allow only one
use strict; use warnings; open(FILE1, /cygdrive/c/cpros/mola.txt); my $line = <FILE1>; print $line; close(FILE1); open(FILE1,
use PHP and MySQL and want to use SELECT statement which date_post(datetime variable) start
I don't have an actual use case for this, but I'm curious, whether there
I have an 'ask_a_question' page with an onbeforeunload function to alert the user before
I am trying to submit a form after a onbeforeunload event in JavaScript and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.