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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:22:28+00:00 2026-06-06T06:22:28+00:00

Is there such a thing? I know that I can hook my function on

  • 0

Is there such a thing?

I know that I can hook my function on the click event of all links, but there are other situations where a page is changed, like refresh or when a different script changes the window.location


In the end, I did it by sending a string trough postMessage from the unload event, like this:

$(window).bind('unload', function(e){
  window.parent.postMessage('unloading');
});

in the parent document:

$(window).bind('message', function(e){     
  if(e.originalEvent.data == 'unloading'){
    // ajax stuff here
  }
});

It appears to work.
I probably should have mentioned that there’s a iframe involved 🙂

  • 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-06T06:22:29+00:00Added an answer on June 6, 2026 at 6:22 am

    There’s the beforeunload event, which is fired when the page is being torn down (either to follow a link, or if the window is being closed, or refresh, etc.). Example:

    window.onbeforeunload = function(event) {
        var s = "You have unsaved changes. Really leave?";
    
        event = event || window.event;
        if (event) {
            // This is for IE
            event.returnValue = s;
        }
    
        // This is for all other browsers
        return s;
    }
    

    There are, for obvious reasons, very strict limits on what you can do in the handler of the beforeunload event, and as you can see above beforeunload handlers have a different signature than normal event handlers. Basically, your code can’t do anything asynchronous, can’t open new windows, and can’t cancel the event. It can return a string, and if it does, the browser will pop up a window asking whether you really want to leave the page, and including your string in that pop-up.

    From your comment on the question:

    I need it before so I can fire a ajax request and update some things…

    The way to do that here many years after the question was originally asked is with the beacon API. This lets you send a non-blocking asynchronous request to the server without slowing down the process of the browser tearing down your page and navigating to the next:

    navigator.sendBeacon("/path/to/notify", optionalData);
    

    It’s a send-and-forget, but the browser doesn’t cancel it when your page is torn down (like it does a standard asynchronous ajax request). Instead, it allows that request to complete even though your page has been removed.

    Back in 2012 when this answer was originally written, you could usually get away with a synchronous ajax call (async: false) provided it didn’t take too long. But you can’t reliably do that now (and it was never a good idea, it holds up the UI).

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

Sidebar

Related Questions

I know there is no such thing as a dumb question but this is:
Can I ship my application with compiled templates? I there such a thing at
Now, I am aware that there is no such thing as exiting an app
In Google App Engine, there is such a thing as a ListProperty that allows
There's such thing as __w64 in Visual C++ 9. I came across it while
Is there such a thing as a Memory profiler for Iphones apps? I'd like
Is there such a thing? It is the first time I encountered a practical
Is there such a thing? Maybe there should be, I don't think Microsoft built
Is there such a thing as an on load complete for images in iphone?
Is there such a thing as JspC in the Seam/JSF/Facelets world? I used the

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.