Closing dialog with js loads incorrect page

Closing dialog with js loads incorrect page

In my app I have a number of dialogs containing forms and when the forms are submitted (using ajax) and done I use $('.ui-dialog').dialog('close'); to close them. If dialogs have been opened from previous pages the wrong page (the parent's parent) is shown when the dialog closes. This also happens if I have multiple dialogs opened from one page. It works fine when I open just one dialog and if I close a dialog with the close button.

Many thanks in advance.

M.