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 7509357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:53:11+00:00 2026-05-29T22:53:11+00:00

When a user leaves a page, I need to ask him if he wants

  • 0

When a user leaves a page, I need to ask him if he wants to perform a particular action before leaving.

<script type="text/javascript">
$(document).ready(function() {
        window.onbeforeunload = askConfirm;
});

function askConfirm() {
    var addFriend = confirm("Would you like to ping?");
    if (addFriend) {
        var xmlhttp;
        if (window.XMLHttpRequest) {
            xmlhttp=new XMLHttpRequest();
        } else {
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.open("GET","http://example.com?ping=1234",true);
        xmlhttp.send();
    }
    return null;
}

Depending on the value of that last return statement ("", null, true, false, or not having a return statement at all) I can have one of two situations:

1) The user gets the “Would you like to ping?” confirmation (good), the ping is sent (good), and the user is presented with a “This page is asking you to confirm that you want to leave” popup (BAD).

-or-

2) The user gets the “Would you like to ping?” confirmation (good), the ping is not sent (BAD), and the user is not presented with a “This page is asking you to confirm that you want to leave” popup (good).

How can I have the AJAX ping sent, yet suppress the “This page is asking you to confirm that you want to leave” popup?

Edit: As ridiculous as it sounds, the “work around” that I found for this issue is to alert() after the xmlhttp.send() statement. The only clean way to do that is to alert the user that his ping has been sent. If future StackOverflowers find a better solution, I would love to know.

Thanks.

  • 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-05-29T22:53:12+00:00Added an answer on May 29, 2026 at 10:53 pm

    well instead of doing it before you leave, I did it onunload and here is what I got to work in FF and IE (besides chrome, that’s all I got): http://jsfiddle.net/rmpLm/9/.

    perhaps you can get it to work on chrome, but if you cant, maybe you could make it so that if you hit ok, it takes you to a page where you send it. hope it helps : )

    EDIT: maybe you should consider making it so it does it onload

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

Sidebar

Related Questions

I need to send a post request when the user leaves the page in
I need to do some processing after my user leaves a text field. I
I need to destroy a session when user leave from a particular page. I
I'm trying to perform a post query when the user leaves the page. The
Basically, once a user leaves a webpage in my application, I need to call
When the user is sent to the thank you page (cart/checkout/complete) I need to
I need to store user preferences on a per page basis in my application.
I've seen many examples how to ask user if he really wants to leave
in my web application if the user leaves the current page without having saved
I need to leave some instructional comments for other developers on a page/user control.

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.