Iframe loading techniques and performance


Iframes are used to display a web page within a web page, load third party content, ads and widgets. The main reason to use the iframe technique is that the iframe content can load in parallel with the main page: it doesn't block the main page. There are two drawbacks in using iframe,
  • Iframes block onload of the main page
  • The main page and iframe share the same connection pool 
The onload blocking is the biggest problem of the two and hurts performance the most. You really want the load event to fire as soon as possible. Here we will see different ways to load the iframe.


Normal Iframe

You all know this one. It's the default way to load an iframe and works in all browsers:

<iframe src="/path/to/file" frameborder="0" width="728" height="90" scrolling="auto">
</iframe>

Using the Normal Iframe technique will result in the following behaviour in all browsers:
  • Iframe starts loading before onload of main page
  • Onload of the iframe fires after all iframe content is done loading
  • Onload of main page fires after the iframe's onload fires: the iframe blocks the main page onload.

Iframe After Onload

If you want to load something in an iframe, but the content of the page depends on main page information, or the content of the iframe is not immediately visible to the user because it's way down below the fold or hidden behind a link/tab. Consider deferring the loading of the iframe until after the main page is done.

<script>
//doesn't block the load event
// Create RunTime IFrame so that it doesn't block the load event function createMaturityScheduleframe() { var i = document.createElement("iframe"); i.src = "../iFramePage.aspx";
    i.frameBorder = "0";
    i.scrolling = "auto";
    i..style.height = "500px";
    i.id = "FrmGeneral";
    i.name = "FrmDetail";
    document.getElementById("DIV_THAT_CONTAINS_IFRAME").appendChild(i);

};
 
</script>
 
The Iframe After Onload technique will consistently show the following behaviour in all browsers:
  • Iframe starts loading after onload of main page
  • Onload of main page fires without interference of the iframe: the iframe does not block the main page onload.

Dynamic Asynch Iframe

Below code will load the iframe and main page asynchronously.

<script> (function (d) { var iframe = d.body.appendChild(d.createElement('iframe')), doc = iframe.contentWindow.document; // style the iframe with some CSS iframe.style.cssText = "position:absolute;width:200px;height:100px;left:0px;"; doc.open().write('<body onload="' + 'var d = document;d.getElementsByTagName(\'head\')[0].' + 'appendChild(d.createElement(\'script\')).src=\'\/path\/to\/file\'">'); doc.close(); //iframe onload event happens })(document); </script>
 

The magic is in the <body onload="">: the iframe has no content initially, so onload of the iframe fires instantly. Then, we create a new script element, set its source to the JS file that loads the content of the iframe, append the script to the HEAD, the iframe content loads without blocking the main page onload. we should see the following behaviour of the Dynamic Asynch Iframe technique consistently in all browsers:
  • Iframe starts loading before onload of main page.
  • Onload of the iframe fires instantly, because the iframe content is loaded after onload.
  • Onload of main page fires without interference of the iframe: the iframe does not block the main page onload because of the <body onload="">

Although iframe is now less common to use in web applications.But support for iframe is still there in HTML 5.

Comments

Popular posts from this blog

Data Bound Controls in ASP.Net - Part 4 (FormView and DetailsView controls)

ASP.net: HttpHandlers

The Clickjacking attack and X-Frame-Options