最近項目組發現在使用showModalDialog彈出窗體中如果包含IFrame對象,則IFrame對象占用的內存資源在窗體關閉后不會釋放。彈出關閉反復多次后,IE瀏覽器內存占用可超過數百M,嚴重時IE瀏覽器報錯,且無法關閉,只能通過殺進程的方式重啟瀏覽器。經測試,使用open方式彈出也存在該問題。
在IE8瀏覽器中,open和showModalDialog彈出的內存占用有差異:
open方式彈出的窗體占用的是一個獨立的iexplorer.exe進程;
showModalDialog方式彈出的窗體使用和父窗體相同的iexplorer.exe進程;
經過搜索,發現解決辦法是在窗體關閉前,從窗體中刪除IFrame對象,代碼如下:
<span style="font-size:18px">var el = document.getElementById("scanIf");el.src="";el.contentWindow.document.write('');el.contentWindow.document.clear();var p = el.parentNode;p.removeChild(el);</span>
但是測試的時候,發現有兩個限制:
1. el.src可能還沒有執行完,就執行后面的語句,如果IFrame中包含的是跨域內容,則會提示沒有權限;
2. 窗體關閉的比腳本執行的快,內存仍然沒有釋放;
經過修改,最終腳本如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD><TITLE></TITLE><BODY onbeforeunload="return unloadHandler();"><IFRAME id="scanIf" width="800px" height="600px" src = "http://www.baidu.com"></IFRAME><SCRIPT type="text/javascript">function unloadHandler(notip) {// 取消窗口關閉時的監聽事件document.getElementsByTagName("BODY")[0].onbeforeunload = null;var el = document.getElementById("scanIf");if (el) {el.src = "";setTimeout(cycleClear, 100);return "提示:請點擊取消按鈕,當前窗口會自動關閉。";}return true;}function cycleClear() {try {var el = document.getElementById("scanIf");if (el) {el.contentWindow.document.write('');el.contentWindow.document.clear();var p = el.parentNode;p.removeChild(el);}window.close();} catch (e) {setTimeout(cycleClear, 100);}}//window.onunload = unloadHandler;</SCRIPT><input type="button" value="remove" onclick="unloadHandler();"></BODY></HTML>
新聞熱點
疑難解答