0

I have a custom list form that opens within a dialog. What I need to do is the following:

  • Override the closing button (X) by prompting the current user to see if they want navigate away from the page and delete their current list item or stay on the page and complete their current list item.

Here's my code below

On Page Load

_spBodyOnLoadFunctionNames.push(ExecuteOrDelayUntilScriptLoaded(fAddOnCloseHandler, "SP.js"));

fAddOnCloseHandler

function fAddOnCloseHandler() {
        /* Find the close button. */
        var oCloseButton = $('.ms-dlgTitleBtns a[title=Close]', window.parent.document);
        $(oCloseButton).click(function () { fValidateBeforeClose(); })

}

When the user clicks on the (X) button, then fValidateBeforeClose() is called

function fValidateBeforeClose() {


    var isValid = ConfirmOnPageExit();
    //set IsValid value
    if (isValid) {

        DeleteListItemById(RecordID);
        SP.UI.ModalDialog.get_childDialog().close();
    }
    else {

        window.parent.document.onabort = function (e) { return true; };
    }
}

ConfirmOnPageExit is just a function that runs the confirm command

function ConfirmOnPageExit() {
            var DeleteItem = confirm("You have unsaved changes on this page and if you leave this page, then your current recorded will be deleted. Would you like to leave this page (OK) or stay on this page (NO)?");
            if (DeleteItem == true) {
                return true;
            }
            else {
                return false;
            }
        }

If the current user selects to stay on the current dialog, then dialog closes. I want the opposite, I want if the user selects to stay on the current dialog, then dialog does not close.

Help please!!!!

  • Replace true with false and false with true in last function return values? Or am I misunderstanding the problem? – Jan Jouke Tjalsma Apr 4 '15 at 6:57
  • In the else section of the fValidateBeforeClose function is where I tried you suggestion. Still didn't work. I think in the else section is where we should prevent the dialog closing – Brandon Michael Hunter Apr 4 '15 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.