Click here to Skip to main content
15,891,607 members
Articles / Programming Languages / Javascript
Tip/Trick

Return value from chrome browser using showModalDialog

Rate me:
Please Sign up or sign in to vote.
4.75/5 (3 votes)
12 Dec 2011CPOL 33.9K   3   1
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)


Written By
Technical Lead eInfochips Pvt Ltd
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalhi , thanks for your post... Please attach an example file t... Pin
MB Seifollahi19-Dec-11 19:17
professionalMB Seifollahi19-Dec-11 19:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.