Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Javascript

Return value from chrome browser using showModalDialog

4.75/5 (3 votes)
12 Dec 2011CPOL 34.4K  
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

JavaScript
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

JavaScript
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.
JavaScript
        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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)