Ben Aston
Ben Aston

Reputation: 55729

Is there a way to detect when a DOM node is about to be removed from the DOM?

Is there a way to detect when a DOM node is about to be removed from the DOM? ie. something like onBeforeNodeRemoved?

I am currently using mutation observers and the node appears to have already been removed by this point.

Edit: The use-case is that an AngularJS 1.x application is being removed from the DOM by AngularJS-unaware code, and I want to detect the DOM node removal and invoke rootScope.$destroy.

Upvotes: 1

Views: 103

Answers (1)

gforce301
gforce301

Reputation: 2984

If I read your question correctly, it sounds like you're saying that by the time you get your mutation observers in place, the node you are trying to watch is already removed. So you never manage to observe it.

If this is the case get your mutation observers into the page and setup before anything else at all. It's the only way I can see that you would be able to observe "everything" that happens.

Upvotes: 2

Related Questions