IE browser IFrame object memory not free solution
- 2020-03-30 03:44:03
- OfStack
Recently, the project team discovered that if an IFrame object is included in a pop-up form using showModalDialog, the memory resources occupied by the IFrame object will not be released after the form is closed. After popup and close repeatedly, the memory of Internet explorer browser can exceed hundreds of M, when serious Internet explorer browser error, and can not be closed, can only kill the way to restart the browser process. In tests, pop-ups in the open mode also had this problem.
In IE8, the open and showModalDialog popups have different memory usage:
An open popup form occupies a separate iexplorer. Exe process.
The showModalDialog window USES the same iexplorer. Exe process as the parent window.
After searching, the solution is to remove the IFrame object from the form before the form closes. 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 when tested, there were two limitations:
1. El. SRC may execute the following statement before it is finished, and if the IFrame contains cross-domain content, it will prompt no permissions;
2. The form closes faster than the script, and the memory is still not free;
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) {
//Cancels the listening event when the window closes
document.getElementsByTagName("BODY")[0].onbeforeunload = null;
var el = document.getElementById("scanIf");
if (el) {
el.src = "";
setTimeout(cycleClear, 100);
return " Tip: please click the cancel button, the current window will close automatically. ";
}
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>