当项目以tab页签方式打开多个iframe窗口时,关闭tab页签同时也需要关闭iframe并释放内存资源,主要是针对IE浏览器。
原生js
/** * 销毁iframe,释放iframe所占用的内存。 * @param iframe 需要销毁的iframe id */function destroyIframe(iframeID){ var iframe = document.getElementById(iframeID);//把iframe指向空白页面,这样可以释放大部分内存。 iframe.src = 'about:blank'; try{ iframe.contentWindow.document.write(''); iframe.contentWindow.document.clear(); }catch(e){} //把iframe从页面移除 iframe.parentNode.removeChild(iframe); }复制代码
Jquery写法
function destroyIframe(iframeID){ var iframe = $('#' + iframeID).prop('contentWindow');$('#' + iframeID).attr('src', 'about:blank');try{ iframe.document.write(''); iframe.document.clear(); }catch(e){} //把iframe从页面移除 $('#' + iframeID).remove(); }复制代码