admin管理员组

文章数量:1314480

This is my first question on here so here goes....

I've got an issue in IE8 where I have a popup form (window.showDialog()) used to edit receipt information in an accounting system.

This was working fine up until I had to add more content by adding a dynamically built table of input fields. I'm getting the information back in an array, however that's where my error seems to be occurring. var pinputs = []; is what seems to be causing the issue.

js function in the popup form:

function saveForm() {
if($('user_id')){
    var user_id = $F('user_id');
} else {
    var user_id = 0;
}
var payments = $$('.payment');
var pinputs = [];
for(var i=0; i<payments.length; i++){
    pinputs.push($F(payments[i]));
}
window.returnValue = {received_of: $F('received_of'), user_id: user_id,
                    note: $F('note'), work_date: $F('work_date'), payment: pinputs};
window.close();
}

js function in the parent js file:

function modifyReceiptInformation(id) {
return window.showModalDialog('mod.php?mod=receipts&mode=receipt_edit_popup&wrapper=no&receipt_id=' + id, 'Modify Receipt',"dialogWidth:600px;dialogHeight:500px");
}

I found a similar situation already on here but that was involving the calling of functions from the child form which I'm not doing here. Perhaps I didn't understand the solution? I'm not an expert with JS so any input will be helpful.

--Edit--

Forgot to also add in here that the var payments = $$('.payment'); is the array of input fields in my template file.

This is my first question on here so here goes....

I've got an issue in IE8 where I have a popup form (window.showDialog()) used to edit receipt information in an accounting system.

This was working fine up until I had to add more content by adding a dynamically built table of input fields. I'm getting the information back in an array, however that's where my error seems to be occurring. var pinputs = []; is what seems to be causing the issue.

js function in the popup form:

function saveForm() {
if($('user_id')){
    var user_id = $F('user_id');
} else {
    var user_id = 0;
}
var payments = $$('.payment');
var pinputs = [];
for(var i=0; i<payments.length; i++){
    pinputs.push($F(payments[i]));
}
window.returnValue = {received_of: $F('received_of'), user_id: user_id,
                    note: $F('note'), work_date: $F('work_date'), payment: pinputs};
window.close();
}

js function in the parent js file:

function modifyReceiptInformation(id) {
return window.showModalDialog('mod.php?mod=receipts&mode=receipt_edit_popup&wrapper=no&receipt_id=' + id, 'Modify Receipt',"dialogWidth:600px;dialogHeight:500px");
}

I found a similar situation already on here but that was involving the calling of functions from the child form which I'm not doing here. Perhaps I didn't understand the solution? I'm not an expert with JS so any input will be helpful.

--Edit--

Forgot to also add in here that the var payments = $$('.payment'); is the array of input fields in my template file.

Share Improve this question edited Jan 24, 2011 at 20:35 Marcel Korpel 21.8k6 gold badges62 silver badges80 bronze badges asked Jan 24, 2011 at 19:55 ValandresValandres 891 silver badge11 bronze badges 4
  • Make sure you move all <meta> tags on the page ABOVE all <script> tags. – Alex Commented Jan 24, 2011 at 20:04
  • Yeah that was one of the first things I found while researching this issue. That's not the cause of my issue. Thanks for the response though :D – Valandres Commented Jan 24, 2011 at 20:06
  • possible duplicate of What causes the error "Can't execute code from a freed script" – Marcel Korpel Commented Jan 24, 2011 at 20:38
  • I checked that post out already as I stated before. I didn't feel that this was the same situation. – Valandres Commented Jan 24, 2011 at 20:39
Add a ment  | 

2 Answers 2

Reset to default 4

You're probably trying to access methods on the array returned by the popup after the popup is closed. That returned array was constructed on the popup, and is dependent on the popup still existing to be usable.

So you have a few options:

  • don't close the popup from within the popup script. Get your parent handler to do what it needs with the array (such as cloning it in an array of its own with [].concat(popupArray) for example), then have it close the popup.

  • convert your array to a string to cross the popup/parent boundary. JSON.stringify()/JSON.parse() would do the job nicely if you don't care about IE6/7. That way, you can still close the popup from within the popup script (apparently, string objects don't get that particular problem with IE.)

I had the same problem, so I wrote this handy function to work around the issue.

// The array problem is when modalDialogue return values are arrays.  The array functions
// such as slice, etc... are deallocated when the modal dialogue closes.  This is an IE bug.
// The easiest way to fix this is to clone yourself a new array out of the remnants of the old.
//
// @param[in] ary
//   The array, which has been passed back from a modal dialogue (and is broken) to clone
// @returns
//   A new, unbroken array.
function cloneArray(ary) {
    var i;
    var newAry = [];
    for(i=0; i<ary.length; i++){
        if(Object.prototype.toString.call(ary[i]) == '[object Array]') {
            newAry.push(cloneArray(ary[i]));
        } else{
            newAry.push(ary[i]);
        }
    }
    return newAry;
}

Then you can use it thusly:

var selectedAry = window.showModalDialog("Window.jsp", inputAry, "dialogWidth:900px; dialogHeight:700px; center:yes; resizable: yes;");
var newAry = cloneArray(selectedAry);

本文标签: