在处理IE iframe父页面和子页面进行数据交换时,出现《SCRIPT5011:不能执行已释放Script的代码》的问题。
按照我遇到的问题为例:(我的页面结构为:父页面中嵌套有iframe子页面)
1、造成这种情况的原因是:父页面初始化声明变量columnData为数组(数组对象是引用类型,赋值传递的是地址),创建iframe子页面后给父页面变量columnData赋值,赋值后销毁iframe子页面,再次调用变量columnData的时候就会抛出异常‘SCRIPT5011:不能执行已释放Script的代码'。
//父页面中
$dialog({
url: "xxx",
title: '请选择',
id: 'column',
transParams: {
parentWin: window,
columnData: this.view['column']//传递给子页面的数据
}
},null)
//iframe子页面中
init: function(){
if(window.transParams.columnData){
this.$set(this, 'selects', window.transParams.columnData || [])
}
var $this = this
this.columns.forEach(function(item){
$this.selects.forEach(function(s){
if(s.id == item.id){
$this.$set(item, 'display', true)
}
})
})
}
//iframe 子页面销毁后 再次调用window.transParams.columnData 会报错
2、解决办法:我们将父页面中的变量columnData定义为字符串(字符串是值类型,赋值传递的是值),子页面iframe中给父页面变量columnData赋值也为字符串,销毁iframe后,调用变量columnData时再解析成数组对象。
//父页面中
$dialog({
url: "xxx",
title: '请选择',
id: 'column',
transParams: {
parentWin: window,
columnData: JSON.stringify(this.view['column'])//传递给子页面的数据
}
},null)
//iframe子页面中
init: function(){
if(window.transParams.columnData){
this.$set(this, 'selects', JSON.parse(window.transParams.columnData) || [])
}
var $this = this
this.columns.forEach(function(item){
$this.selects.forEach(function(s){
if(s.id == item.id){
$this.$set(item, 'display', true)
}
})
})
}
这样就没问题了
总结:父页面传递给子页面以字符串的形式传递,子页面将其再转化为对象;子页面传递给父页面以字符串形式传递,父页面再将其转化为对象。
以此记录解决ie11 SCRIPT5011:不能执行已释放Script的代码问题。