admin管理员组

文章数量:1405170

I need to close the modal dialog with return a value in a same function

I cannot write code as follow,because when a returning a value, next line never be excuted,

function butOK_OnClick() {
    return "OK";
    window.close();
}

so is this the right way ?

function butOK_OnClick() {
    window.returnvalue = "OK";
    window.close();
}

or what is the best way to do this ?

I need to close the modal dialog with return a value in a same function

I cannot write code as follow,because when a returning a value, next line never be excuted,

function butOK_OnClick() {
    return "OK";
    window.close();
}

so is this the right way ?

function butOK_OnClick() {
    window.returnvalue = "OK";
    window.close();
}

or what is the best way to do this ?

Share Improve this question edited Jan 17, 2013 at 14:20 Madura Harshana asked Jan 17, 2013 at 14:15 Madura HarshanaMadura Harshana 1,2998 gold badges25 silver badges40 bronze badges 5
  • PopUp? You using a modal dialog? – epascarello Commented Jan 17, 2013 at 14:16
  • Are you just trying to run a function when the popup closes? – romo Commented Jan 17, 2013 at 14:18
  • thats my mistake. yes, thats the modal dialog – Madura Harshana Commented Jan 17, 2013 at 14:19
  • I need to return a value when closing a modal dialog – Madura Harshana Commented Jan 17, 2013 at 14:19
  • If you want to return a value calculated in the other window to the main window (from which the popup opened), do window.opener.[...] = [...] before closing – marekful Commented Jan 17, 2013 at 14:20
Add a ment  | 

2 Answers 2

Reset to default 2

Assuming you're using window.showModalDialog to open the window (since window.open does not allow for return values), you'd just set the returnValue property of the modal and then set it to a variable in the opener.

Opener window:

var returnedValue = window.showModalDialog(url);

Modal window:

window.returnValue = 'foo';
window.close();

you can use,

$("#modalId", window.top.document).data("cancelled", true);
$("#modalId", window.top.document).data("returnValue", returnVal);

本文标签: Close modal dialog with returning a value in javascriptStack Overflow