简体   繁体   中英

“Can't execute code from a freed script” in IE8 using Javascript w/Prototype

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM