How to return array object from showmodaldialog window in IE and Chrome browser?

1.7k Views Asked by At

From showmodaldialog window.returnValue in Firefox I am getting array object, but in IE and Chrome getting error.

function doYes() {
    var val=[];
    val.push("A");
    val.push("B");
    window.returnValue =val;
}

Even array works in Firefox not in IE9. Please see the piece of code in showModalDialog box.

function doYes() {
    var val={};
    val.x="A";
    val.y="B";
    window.returnValue =val;
}

This also works in Firefox not in IE9.

1

There are 1 best solutions below

1
On

Since Chrome37, showModalDialog has been obsoleted. If you're using older Chrome, please read this SO answer.

In IE11: If dialog's return value is either an array or an object, it can't access the properties in its prototype. This causes for example alert(modal_return_value) to fail and trigger SCRIPT5011 Can't execute code from a freed script error (alert uses toString() method in the prototype).

Somehow this is even understandable since the prototype actually is in a window, which was closed before you will access these properties. I'm not sure if this is a bug or an intended feature in IE11, but either way it's annoying.

You can access to the own properties of the returned object though. For example you can iterate through an array using a for loop, and even alert(modal_return_value[n]) would work.

Or you can create a new array from the return value, something like this:

var arr = showModalDialog('returnsArray.htm');
if (!arr) {/* Dialog cancelled, do something */}
arr = Array.prototype.splice.call(arr, 0);

In the last line we borrow the splice method of Array.prototype and create a new array from the returned array.