JavaScript – Passing Values To/From ModalDialog

05.07.2009 at 14:34 (JavaScript, Web programming) (, , , , , , , , )

function openWin( txtFieldId) {
var obj=new Object();
obj.Name = „parent“;
var tst;
if (window.showModalDialog) {
tst = window.showModalDialog(„../faces/pages/nomen.jsp“, obj, „height=5000, width=400″ );
} else {
tst = window.open(„../faces/pages/nomen.jsp“, obj, „height=5000, width=400, modal=yes“);
}
if(window.opener == null){
window.opener = window.self;
}

// alert(tst.ChildName);
// TextFiled in parent window
if(document.getElementById(txtFieldId) != null){
document.getElementById(txtFieldId).value = tst.ChildName;
}
}

function setValsBackToParent(val)
{
var obj = new Object();
obj.ChildName = val;
window.returnValue = obj;
}

function getValuesFromParent(fieldToShowId)
{
var obj = window.dialogArguments;
if(document.getElementById(fieldToShowId) != null){
document.getElementById(fieldToShowId).value = obj;
}
}

function closeWin(){
window.focus();
window.opener = self;
window.close();
}

JSF Code Snippet:
in parent window
<h:inputText  id=“InputNom“ />
<h:commandButton value=“#{msg.choose}“ onclick=“openWin( ‘InputNom’);“ />

in modal dialog
<h:inputText  id=“showValField“ />
<h:commandButton id=“showVals“ value=“#{msg.show}“ onclick= „getValuesFromParent(’showValField’); />
<h:commandButton id=“closeWin“ onclick=“setValsBackToParent(‘PassBackToParentValue’);closeWin();“ value=“#{msg.close}“ />

Пусни/изпрати коментар