65.9K
CodeProject is changing. Read more.
Home

Return value from chrome browser using showModalDialog

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.75/5 (3 votes)

Dec 12, 2011

CPOL
viewsIcon

34382

We are not able to get return value from chrome browser directly, because chrome is not understand then window.returnValue directly. We have to create one object and assign all values into that object and that object will pass into open window params as below
function ShowModal() {
        var forename = document.getElementById("forename");
        var surname = document.getElementById("surname");

        var sharedObject = new Object();
        sharedObject.forename = forename.value;
        sharedObject.surname = surname.value;

        if (window.showModalDialog) {
          var retValue = showModalDialog("model.html", sharedObject, "dialogWidth:200px; dialogHeight:200px; dialogLeft:300px;");
          if (retValue) {
            UpdateFields(retValue.forename, retValue.surname);
          }
        }
        else {
          // for similar functionality in Opera, but it's not modal!
          var modal = window.open("model.html", null, "width=200,height=200,left=300,modal=yes,alwaysRaised=yes", null);
          modal.dialogArguments = sharedObject;
        }
}
after that we are able to get that object on model window, and assign that value to textbox or any othr container
        var sharedObject = window.dialogArguments;  
        var forename = document.getElementById("forename");
        var surname = document.getElementById("surname");
        forename.value = sharedObject.forename;
        surname.value = sharedObject.surname;
if we want to return value from the model value need to do code as below, UpdateFileds function must into parent window.
        var forename = document.getElementById("forename");
        var surname = document.getElementById("surname");

        if (window.showModalDialog) {
          var sharedObject = {};
          sharedObject.forename = forename.value;
          sharedObject.surname = surname.value;

          window.returnValue = sharedObject;
        }
        else {
          // if not modal, we cannot use the returnValue property, we need to update the opener window
          window.opener.UpdateFields(forename.value, surname.value);
        }
        window.close();
       
function UpdateFields(newFore, newSur) {
        var forename = document.getElementById("forename");
        var surname = document.getElementById("surname");
        forename.value = newFore;
        surname.value = newSur;
      }
Note :Run code using virtual directory only other wise it is not going to work. Enjoy Coding