解决IE10报错“不能执行已释放script的代码”

在项目的兼容性测试中,有遇到一个“不能执行已释放script的代码”的报错,只在IE中会触发,谷歌和火狐测试都是没有问题。网上查找原因,发现是父窗口调用子iframe窗口,然后子页面操作父页面的数据,在IE中,当子页面关闭时,会自动回收子页面的js等,从而导致出现此现象。

解决方法:

将子页面返回过来的父页面数据,用深度克隆一份(即改变引用地址,不再是原来返回的数据),再关闭子页面。

其中getFormData()是子页面的函数,返回json对象数据

//深度克隆对象
function copy(obj){
    return Object.assign({}, obj);
}

还要重写Object.assign方法兼容ie

//重写Object.assign,兼容ie10
if (typeof Object.assign != 'function') {
    Object.assign = function(target) {
        'use strict';
        if (target == null) {
            throw new TypeError('Cannot convert undefined or null to object');
        }
        target = Object(target);
        for (var index = 1; index < arguments.length; index++) {
            var source = arguments[index];
            if (source != null) {
                for (var key in source) {
                    if (Object.prototype.hasOwnProperty.call(source, key)) {
                        target[key] = source[key];
                    }
                }
            }
        }
        return target;
    };
}

猜你喜欢

转载自blog.csdn.net/a1085578081/article/details/108861922