Js iframe memory release and highly adaptive example

Source: Internet
Author: User

I. Release iframe memory

According to relevant materials, IE has a bug in the collection of iframe elements. In general, you should change the src attribute value of this element to "abort: blank ", manually remove it from the DOM tree, and leave the variables referenced in the script empty and call CollectGarbage () to avoid memory leakage caused by the failure of iframe collection.

Function clearRAM (){
Var frame = document. getElementById ("ifr_content ");
Frame. src = 'about: blank ';
Frame.contentcontentdoc ument. write (''); // clear the frame content
Frame.contentmediaworkflow doc ument. clear ();
Frame. contentWindow. close (); // prevent frame memory leakage

If (navigator. userAgent. indexOf ('msie ')> = 0 ){
If (CollectGarbage ){

CollectGarbage (); // dedicated to releasing memory for IE

// Delete the original tag
Var tags = document. getElementById ("ifrSet ");
Tags. removeChild (frame );

// Add a frameset framework
Var _ frame = document. createElement ('framework ');
_ Frame. src = '';
_ Frame. name = 'content ';
_ Frame. id = 'ifr _ content ';
Tags. appendChild (_ frame );
         }
  }

 }

// Active release once every 5 seconds
SetInterval (function (){
If (navigator. userAgent. indexOf ('msie ')> = 0 ){
If (CollectGarbage ){
// Alert (1)
CollectGarbage (); // dedicated to releasing memory for IE
    }
  }
},5000 );

II. iframe highly adaptive

Many people have been plagued by the highly adaptive problem of iframe, which has been encountered many times in the project. There are also a lot of related code on the Internet, but they cannot meet their own requirements.
This code mainly solves the problem that the outermost scroll bar dynamically changes with the iframe height. If the iframe height is relatively large, the outermost layer will display a scroll bar, otherwise it will not. The problem with many examples on the internet is that iframe only keeps the maximum height so that the page content height is very small, but there is a scroll bar on the right.
Compatibility: Google, Firefox, ie8 +

/**
* Iframe adaptive height, which is the minimum manual height
*/
Function initIframeHeight (height ){
Var userAgent = navigator. userAgent;
Var iframe = parent.doc ument. getElementById ("contentIframe ");
Var subdoc = iframe. contentDocument | iframe.content?#doc ument;
Var subbody = subdoc. body;
Var realHeight;
// Special processing by Google
If (userAgent. indexOf ("Chrome")>-1 ){
RealHeight = subdoc.doc umentElement. scrollHeight;
    }
Else {
RealHeight = subbody. scrollHeight;
    }
If (realHeight $ (Iframe). height (height );
    }
Else {
$ (Iframe). height (realHeight );
    }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.