Home >Web Front-end >JS Tutorial >Solution to the problem of IFrame object memory not being released in IE browser_javascript skills
Recently, the project team discovered that if the pop-up form using showModalDialog contains an IFrame object, the memory resources occupied by the IFrame object will not be released after the form is closed. After the pop-up and closing are repeated many times, the memory occupied by the IE browser can exceed hundreds of MB. In severe cases, the IE browser reports an error and cannot be closed. The only way to restart the browser is to kill the process. After testing, this problem also exists when using the open method to pop up.
In IE8 browser, there is a difference in memory usage between open and showModalDialog pop-ups:
The form that pops up in open mode occupies an independent iexplorer.exe process;
The form popped up by showModalDialog uses the same iexplorer.exe process as the parent form;
After searching, I found that the solution is to delete the IFrame object from the form before closing the form. The code is as follows:
<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>
But during testing, I found two limitations:
1. el.src may not be executed yet, and the following statements will be executed. If the IFrame contains cross-domain content, it will prompt that there is no permission;
2. The form is closed faster than the script is executed, and the memory is still not released;
After modification, the final script is as follows:
<!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>