Alwyn
Alwyn

Reputation: 8337

Detect when DOM is unloaded

Is there a way to detect when a DOM has been unloaded (destroyed/removed/GCed etc.)?

I have a global hub where I subscribe to a bunch of listeners (using reactive subject). I need to detect when a relevant DOM has been destroyed and unsubscribe listeners that pertain to the now destroyed DOM.

Upvotes: 0

Views: 679

Answers (2)

Orlando
Orlando

Reputation: 9712

If you are removing DOM using jQuery you can use the remove event:

$("#myDiv").on("remove", function () {
    //code here
})

Vanilla jQuery does not trigger remove event. This behaviour depends on jQuery UI being loaded together with jQuery. Instead of jQuery UI, you can extend the .remove() method, https://stackoverflow.com/a/18410194/368691.

Upvotes: 1

Geuis
Geuis

Reputation: 42297

Like the load event, there is an unload event. https://developer.mozilla.org/en-US/docs/DOM/window.onunload

Upvotes: 2

Related Questions