Recently I had to perform some actions before opening and before closing a dialog in a website. I had multiple dialogs throughout the website, the thing in common among them, was that all dialogs used the awesome jQueryUI dialog plugin, so I could take advantage of that..
Digging a bit into the jQueryUI source code, we can find the open and close dialog methods, they are responsible for showing and hiding the dialogs, respectively. Overriding their behavior, without losing any functionality is easy by simply setting them again, and calling the old method again, with all previous parameteres.
The idea is simple, store the old open and close methods in a variable, redefine the dialog methods, do whatever we want to and call the old method again, using the stored methods, with the same parameters and scope, keeping all functionality.