Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I want to validate custom modal dialog form and if its not ok, prevent form from closing. For this purpose, I attached to X-close-dialog-button. Problem is to prevent it's default behavior.

function fValidateBeforeClose(event) {
    event.stopImmediatePropagation(); // Not working
}    
function fAddOnCloseHandler() {
    var oCloseButton = jQuery("a[id^='DlgClose']", parent.document);
    oCloseButton.on( "click", function (event) { fValidateBeforeClose(event)} ); 
}       
_spBodyOnLoadFunctionNames.push( ExecuteOrDelayUntilScriptLoaded(fAddOnCloseHandler, "SP.js") );
share|improve this question

3 Answers 3

up vote 3 down vote accepted

Good way to remove handlers from dialog's close button is using $clearHandlers Method.

Check code below:

function fValidateBeforeClose(event) {
    var isValid = true;
    //set IsValid value
    if(isValid)
    {
       //close dialog
       SP.UI.ModalDialog.get_childDialog().close();
    }
    else
    {
      alert('Not valid');
    }
}  

function fAddOnCloseHandler() {
    var oCloseButton = $("a[id^='DlgClose']");

    $clearHandlers(oCloseButton.get(0));

    $(oCloseButton).on("click", function (event) { fValidateBeforeClose(event)} );
}   

_spBodyOnLoadFunctionNames.push( ExecuteOrDelayUntilScriptLoaded(fAddOnCloseHandler, "SP.js") );
share|improve this answer
    
Worked. I modify your answer, to remove unnecessary element search in DOM. –  dbardakov May 22 '13 at 11:49
    
Ok, I approved it. –  Andrew May 22 '13 at 12:01

I think you should remove old event handler

 var js = oCloseButton.attr("onclick");
 oCloseButton.attr("onclick", "");//remove sharepoint click handler...
share|improve this answer
    
Good catch,jQuery(oCloseButton).off().find("*").off(); seems working. Though, I liked "the SharePoint way" provided by @Andrew better. –  dbardakov May 22 '13 at 11:48

In SharePoint 2013, this works fine after I added "window.parent.document" in the following line:

var oCloseButton = $("a[id^='DlgClose']",window.parent.document);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.