preventing beforeonpagechange when form is dirty

preventing beforeonpagechange when form is dirty

Ok, so catching a dirty form is easy enough... BUT... how do you catch, prompt and then either deny or allow a page change?

The use case is this: Users navigates to profile page, makes a change and then navigates away without saving and then complains because form (profile picture) is not saved.

I've tried onbeforepagechange and pageremove and both are getting executed regardless of my best attempts to intercept, check my dirty flag and prompt for permission to abandon.

Help!