5

Is there any way that I can define a destructor for an object in JavaScript that will be called automatically when object is discarded?

Im creating my obj like so:

function SomeObject(){
    this.test = null;

    this.method = function(){
      alert("Testing Method");
    };

    this.destroy = function(){
      // Destroy stuff here
    };
}

var test = new SomeObject();

I can call destroy when needed but when the user exits the page I cant call destroy. The reason I need to do this is that I call functions in php using ajax that saves session data. I would like it to destroy the particular session data when im done with that particular js object.

Any ideas?

Adam Magaluk
  • 1,716
  • 20
  • 29
  • Check out http://stackoverflow.com/questions/1631959/browser-window-close-event – Demian Brecht Aug 26 '11 at 18:32
  • if you have a destroy() method, why don't you just call it when you're done with the object? – Einacio Aug 26 '11 at 18:34
  • Einacio, I will but but if the user exits the page or changes pages within the application I cant call it. I will try the window.unload though. – Adam Magaluk Aug 26 '11 at 18:38
  • No. There is no such thing as a "destructor" in JavaScript and using onunload/onbeforeunload is *not reliable* across browsers/situations (consider history navigation or refresh as simple counter-examples). Consider alternative approaches. –  Aug 26 '11 at 18:42

3 Answers3

3

You can't use a deconstructor for an object, but you can use window.onbeforeunload or window.onunload to do any last-moment adjustments.

If you return a string from onbeforeunload it will prompt the user that string as a confirm dialog as to whether they want to leave. This is useful primarily if you want to prompt the user to save their work or some similar state-preserving action.

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • 1
    Typically one should use `onbeforeunload` to prompt a user to save, and `onunload` when you *only* need to finish some state-preserving requests (settings cookies, localStorage, etc). I'm pretty certain `onbeforeunload` is supported by all modern browsers and IE7+ (test it and let me know). – zzzzBov Aug 26 '11 at 18:38
  • 1
    @zzzzBov `onbeforeunload` is supported by all browsers (even IE6) except Opera. – duri Aug 26 '11 at 18:39
  • @zzzzBov unfortunately it depends upon what causes the "unload" :( –  Aug 26 '11 at 18:39
  • YOUR WHOLE APP IS UNLOADED WHEN THE WINDOW IS UNLOADED. – Resist Design Dec 23 '16 at 04:05
  • @ResistDesign, I don't understand what point you're trying to make with that comment. Please consider using a civil tone and attempt to make an actual point. There are a variety of ways that page state can persist after a window is closed which it seems that you're not aware of. – zzzzBov Dec 23 '16 at 05:26
2

You can call that function on the window.onunload or window.onbeforeunload events. But be aware that this might be called even when the user navigates within your own application or when the back and forward buttons are used.

Community
  • 1
  • 1
Jordão
  • 55,340
  • 13
  • 112
  • 144
  • +1 Because this was the only answer to mention some caveats (although I think it's not exclusive). –  Aug 26 '11 at 18:40
  • That works well for me, I want it to be destroyed when they refresh or the back button is pressed. – Adam Magaluk Aug 26 '11 at 20:09
2

You can bind to the unload event:

function SomeObject(){

    ...

    var that = this;
    $(window).unload(function() {
        that.destroy();
    });
}

This way your destroy method will be called when the user quits the page.

Without jQuery you can bind the event with this:

var event = 'load';
var handler = function() { ... };
if (window.addEventListener) {
    window.addEventListener(event, handler, false);
} else {
    window.attachEvent('on'+event, handler);
}
Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194