解決IE11出現異常SCRIPT5011:不能執行已釋放Script的代碼


功能概述:

最近做了一個教育科研系統,由於時間比較緊,所以能集成的功能都盡量做到了一起,其中一個上傳附件的功能,在基類控制器BaseController 中建了一個Action返回視圖,其他需要上傳附件的地方都統一調用該視圖彈出模態窗口window.showModalDialog,返回上傳文件的路徑和數據庫表存的該附件路徑的guid,在調用頁面將返回附件的guid保存到數據表中:

遇到的問題:

該功能在火狐瀏覽器中完全正常,但是在IE11瀏覽器(其他版本未測試)中出現:不能執行已釋放Script的代碼 的異常。但是實際上值已經取到,但是直接判斷是否為null等時報異常:

解決方案:

將彈出窗口返回的數組對象改為字符串,回到調用頁面時再解析成對象(對象是引用類型:的賦值傳遞的是地址,字符串是值類型:賦值是傳遞的值)。

調用頁面js代碼:

 1  /*
 2    * 上傳 
 3    * typeFlag:1-文件,2-圖片,3-三維動畫
 4    */
 5     function MyUpload(dom, guid, typeFlag) {
 6         var obj = new Object();
 7         var strResult = window.showModalDialog('/Base/ShowUploadView?typeFlag=' + typeFlag, obj, "dialogWidth=600px;dialogHeight=300px;");
 8         if (strResult != null && strResult != undefined && strResult != "" && strResult.length > 0) {
 9             var ArrResult = eval(strResult);
10             if (ArrResult[0].guid != null && ArrResult[0].guid != undefined && ArrResult[0].guid != "") {
11                 $.ajax({
12                     url: '/InstrumentShare/UpdateContractAttachmentData',
13                     type: 'post',
14                     dataType: 'text',
15                     data: { 'headID': guid, 'filesID': ArrResult[0].guid },
16                     success: function (result) {
17                         if (result === ""||result===null) {
18                             if (dom.nextElementSibling.nodeName === "A") {  //判斷是否是a標簽
19                                 dom.nextElementSibling.href = ArrResult[0].filesPath;
20                                 dom.nextElementSibling.style.display = ""; //顯示下載a標簽,沒有附件時是隱藏狀態
21                             }
22                             alert("保存附件成功!");
23                         }
24                         else {
25                             alert("保存附件失敗:" + result);
26                         }
27                     },
28                     error: function (result) {
29                         alert("程序異常:保存附件失敗!");
30                     }
31                 });
32             }
33 
34         }
35     }

 

其中視圖頁面代碼:

  1 @{
  2     ViewBag.Title = "上傳附件";
  3     Layout = "~/Views/Shared/_Layout-nofooter.cshtml";
  4 }
  5 
  6 <script src="~/Scripts/UploadsFile/jquery.uploadify-3.1.min.js" type="text/javascript"></script>
  7 <link href="~/Scripts/UploadsFile/uploadify.css" rel="stylesheet" />
  8 
  9 
 10 <div class="modal-header" style="height:50px;">
 11     <h4 id="myModalLabel">上傳附件</h4>
 12 </div>
 13 <div class="modal-body">
 14     <table class="basicTable">
 15         <tr>
 16             <td class="edu_width100">上傳附件:</td>
 17             <td id="attachmentName"></td>
 18         </tr>
 19         <tr>
 20             <td>
 21                 <input type="file" id="file_upload" name="file_upload" />
 22                 <span></span>
 23             </td>
 24             <td></td>
 25         </tr>
 26     </table>
 27     @*<div class="row">
 28         <div class="col-lg-4">上傳附件:</div>
 29         <div class="col-lg-6">
 30            <span id="attachmentName"></span>
 31         </div>
 32         <div class="col-lg-2"></div>
 33     </div>
 34     <div class="row">
 35         <div>
 36             <input type="file" id="file_upload" name="file_upload" />
 37             <span></span>
 38         </div>
 39     </div>*@
 40 </div>
 41 <div class="modal-footer">
 42     <button class="scbtn addBtn" aria-hidden="true" onclick="GetRetuValue()">
 43         保存
 44     </button>
 45     <button class="scbtn btn-warning" aria-hidden="true" onclick="CancelSelect()">
 46         取消
 47     </button>
 48 </div>
 49 
 50 
 51 <script type="text/javascript">
 52     // 窗口返回值
 53     var strResult;
 54     var typeFlag = '@ViewBag.typeFlag';//文件類別:1-文檔文件,2-圖片文件,3-三維動畫
 55     var filesType = '*.*';//上傳文件類型
 56 
 57     /*
 58     * 附件上傳
 59     */
 60     $(function () {
 61         //文件
 62         if (typeFlag === '1') {
 63             filesType = '*.doc;*.txt;*.ppt;*.xls;*.docx;*.xlsx;*.rar;*.zip';
 64         }
 65         //圖片
 66         else if (typeFlag === '2') {
 67             filesType = '*.jpg;*.jpeg;*.png';
 68         }
 69         //三維動畫
 70         else if (typeFlag === '3') {
 71             filesType = '*.swf;*.flv;*.gif';
 72         }
 73         else {
 74             alert("文件類型初始化錯誤!");
 75             window.close();
 76         }
 77     })
 78     function uploadfile() {
 79         $('#file_upload').uploadify('upload', '*');
 80     }
 81 
 82     function Clearupload() {
 83         $('#file_upload').uploadify('cancel', '*');
 84     }
 85 
 86     /*
 87     * 圖片附件上傳
 88     */
 89     $(function () {
 90         var strPath = '';
 91         $('#file_upload').uploadify({
 92             'swf': '@Url.Content("~/Scripts/UploadsFile/uploadify.swf")',
 93             'buttonText': '添加附件',
 94             'auto': true,
 95             multi: true,
 96             //'uploadLimit': 1,
 97             //'queueSizeLimit': 1,
 98             'fileTypeExts': filesType,
 99             'formData': { 'typeFlag': '@ViewBag.typeFlag' },//此處修改附件類型
100             'uploader': '/Base/uploadsAttachment',
101             'fileSizeLimit': '100MB',
102             'onSelectError': function (file, errorCode, errorMsg) {
103                 switch (errorCode) {
104                     case -100:
105                         alert("上傳的文件數量已經超出系統限制的" + $('#file_upload').uploadify('settings', 'queueSizeLimit') + "個文件!");
106                         break;
107                     case -110:
108                         alert("文件 [" + file.name + "] 大小超出系統限制的" + $('#file_upload').uploadify('settings', 'fileSizeLimit') + "大小!");
109                         break;
110                     case -120:
111                         alert("文件 [" + file.name + "] 大小異常!");
112                         break;
113                 }
114             },
115             'onUploadComplete': function (file) {
116                 Clearupload();
117             },
118             'onQueueComplete': function (data) {
119                 Clearupload();
120                 //$("#Attachment").val($("#Attachment").val() + ";" + strPath);
121             },
122             'onUploadSuccess': function (file, data, response) {
123                 var result = eval('(' + data + ')');
124                 if (result.Success) {
125                     if (result.guid != undefined && result.guid != 'undefined') {
126                         //獲取返回數據
127                         aResult = new Array();
128                         //eval({ guid: "ae3c9336-1101-49fd-8116-755cef6bbd27", filesPath: " '/Base/MyDownLoad?Filepath='/UpLoadFile/images/b648fdd0-19d5-4e78-8435-db2ff8d2d147.jpg" })
129                         //aResult={'guid: "'+ result.guid+'", filesPath: "/Base/MyDownLoad?Filepath='+result.filesPath+ '"'};
130 
131                         aResult.push({ guid: result.guid, filesPath: '/Base/MyDownLoad?Filepath=' + result.filesPath });
132                         strResult = JSON.stringify(aResult);
133                         $("#attachmentName").text(result.FileName);
134                         alert("添加附件成功!");
135                     }
136                 } else {
137                     alert(result.Message);
138                     return false;
139                 }
140                 return true;
141             },
142             onUploadError: function (file, errorCode, erorMsg, errorString) {
143             }
144         });
145     });
146 
147     /*
148     * 刪除附件
149     */
150     function DeleteAttachment(attachmentID, fileName, typeFlag) {
151         if (confirm("您確定要刪除附件 【 " + fileName + "】 嗎?")) {
152             $.ajax({
153                 url: '/Base/DeleteAttachment',
154                 type: 'post',
155                 datatype: 'text',
156                 data: { attachmentID: attachmentID, attachmentID: attachmentID, fileName: fileName, typeFlag: typeFlag },
157                 success: function (rel) {
158                     var data = eval(rel);
159                     if (data.Success) {
160                         //移除元素,隱藏控件賦值
161                         //$('#span_' + attachmentID).remove();
162                     }
163                     alert(data.Message);
164                 }
165             });
166         }
167     }
168 
169     /*
170 * 獲取選中值關閉dialog窗口並返回選中值(置於彈出窗口頁面中):待修改
171 */
172     function GetRetuValue(result) {
173         //彈出框返回值
174         window.returnValue = strResult;
175         window.close();
176     }
177 
178     /*
179    * 獲取選中值關閉dialog窗口並返回選中值(置於彈出窗口頁面中):待修改
180    */
181     function CancelSelect() {
182         //彈出框返回值
183         window.returnValue = "";
184         window.close();
185     }
186 </script>
187 <!-- ModalEnd -->

 

引用:http://blog.sina.com.cn/s/blog_6ac4c6cb0101is29.html

在IE6,IE7,IE8,chrome,firefox一些window主流的瀏覽器上,都可以正常運行,就是IE9上出現這個問題,其實,我們用模態方式打開個窗口,對於返回對象使用“==”與字符串比較時出現錯誤。原因是生成對象的窗口已經被銷毀了,這個對象的valueOf與toString等方法調時會出現上述錯誤,一開始的情況是:

 

我使用 json 變量接收返回值 ,返回對象格式如 {id:1,name:senly,age:26}

var json = 模式窗口返回值 ( window.showModalDialog(url,...) )

返回之后我會做判斷

if(IsEmpty(json )) return;

 

IsEmpty方法貼上


function IsEmpty(obj){
     if(typeof(obj)=="undefined"||obj==null||(typeof(obj)!="object"&&(obj+"").replace(/ /g,"")==""|| obj=="null" || obj.length==0)){
          return true;
     }
     return false;
}

 

就是這個處理,報出了異常 SCRIPT5011:不能執行已釋放Script的代碼

因為腳本使用了==去做判斷,所以在IE9下是不允許的,故此,我們可以這么處理:

try{

    if(IsEmpty(json.id))

    return;

}catch(err){

    return;

}

 

注意:

 

雖然以上介紹了該異常出現的原由,以及對該異常進行的容錯處理,但是,假如希望通過IE9,IE10,來實現模態窗口的數據回傳,那又應該如何正確獲取到數據呢?

 

首先,對於字符串型/布爾型的數據回調,ie9/10都是支持的,例如

 

window.returnValue = “我是模態窗口返回的數據”;

 

或者

 

window.returnValue = true;

 

最終在父窗口的通過

 

var json = 模式窗口返回值 ( window.showModalDialog(url,...) )

 

獲取的json都是無誤的,例如

 

 “我是模態窗口返回的數據”或是 true

 

但是,對於返回對象型的數據時,則會出現異常 不能執行已釋放Script的代碼

 

例如,返回對象格式如 {id:1,name:senly,age:26}

 

那么肯定不行,這時候我們可以這么來處理,在返回數據的時候,把對象數據拼接為字符串,然后在父窗口接收到之后,通過eval把字符串轉化為對象,例如:

 

在模態窗口返回:

var dataStr = "{id:1,name:'senlypan'}";

window.parent.window.returnValue = dataStr ;

 

最終在父窗口解析:

var json = 模式窗口返回值 ( window.showModalDialog(url,...) )

// 模態窗口,處理取消/關閉 [undefined]
try{

    if(IsEmpty(json))return;

}catch(err){

    return;

}

// 模態窗口,處理returnValue  [對象銷毀],對象不兼容,使用str轉換為對象:
var obj= eval_r('('+ json +')');

 

此時,就可以通過對象獲取屬性值了,例如通過obj.id或者obj.name,可以得到 1 或者 senlypan


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM