I have a layouts page which creates a new site. I use SP.UI.ModalDialog to show the site. I've created the following js function I use to pop it up:

function PagePopupAndRedir(fileUrl, fileTitle, fileWidth, fileHeight) {
    var options = SP.UI.$create_DialogOptions();
    options.url = fileUrl;
    options.title = fileTitle;
    options.allowMaximize = false;
    options.showClose = true;
    options.width = fileWidth;
    options.height = fileHeight;
    options.dialogReturnValueCallback = modalDialogClosed;


the modalDialog method isn't very relevant here, but it starts like this:

function modalDialogClosed(dialogResult, returnValue) {
    if (dialogResult == '1') {
        window.location.href = returnValue;

In my layouts page, the last thing I do is:

var response = "<script type='text/javascript'>SP.UI.ModalDialog.commonModalDialogClose(1, 'myUrl')</script>";

I can see that the js method is called, but the problem is that the returnValue is never set, it's always undefined. However, returnValue is set in the js-method, but it's not passed based on what I pass, it's passed "directly" from the dialog. I.E. if dialog is correctly closed, it's '1', if it's aborted the value is '0'.

How do I pass parameteres from my server side code to the js-code?

Update: I have observed the funny feature: It SOMETIMES work! About 1 in a few, the parameter is actually passed through to the js-method. Nothing is changed in neither js or cs-code! WTF?


I solved the problem / Found the bug.

I neglected to tell you that a SPLongOperation were running inside the modal dialog. I think that might have had an impact, especially since a part of the code were actually closing the dialog after the LongOperation were finished (DOH!). No wonder parameters weren't set.

I still weren't able to write js to the resonse like I'm trying in the serverside code above, but fortunately the SPLongOperation.End() took a script parameter. I sent my SP.UI.ModalDialog.commonModalDialogClose(1, 'myUrl') as this parameter, and it worked.

| improve this answer | |

Given that it sometimes works, it is probably just a timing issue. Can you try putting it in the onload event and see if it works?

var response = "<script type='text/javascript'>window.onload = function() {SP.UI.ModalDialog.commonModalDialogClose(1, 'myUrl');}</script>";
| improve this answer | |
  • I'm sorry to say it doesn't make a difference. – tarjeieo Oct 3 '11 at 6:43
  • I haven't used the dialog framework to do what you are trying to do, and I'm suspecting that clearing the response is either negating something important, or client script just doesn't have time to run before the dialog is closed. I would recommended just using jQuery to make an ansynch request to the page, and sending the url in the reponse. I have used that approach several times without any problems. – Jon Lester Oct 3 '11 at 12:40

Your Answer

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

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