Author: Manuel Lemos
Updated on: 2013-04-21
Posted on: 2010-10-20
Package: Fast Page Content Loader
Contents
Introduction
Determine which content loads slowly
Setup
Delaying slow content
Loading slow content at the end of the page
Browser compatibility issues
Introduction
Determine which content loads slowly
Setup
<script type="text/javascript" src="contentLoader.js"></script>
Do we want to see debug messages in the browser console or alert window?
<script type="text/javascript"><!--
var cl = new ML.content.contentLoader();
cl.debug = true;
cl.delayedContent = '<div style="text-align: center">' +
'<img src="http://ajaxload.info/images/indicator.gif"' +
' width="32" height="32"></div>';
// --></script>
Delaying slow content
Now that you setup the content loader object, you can replace the slow content tags to delay its load. Lets say for instance that you have a Twitter "Like" widget defined by HTML like this:You need to convert this HTML to tags with JavaScript code like this:
<a href="http://twitter.com/share" class="twitter-share-button"
data-count="horizontal" data-via="jsclasses">
<img src="http://ajaxload.info/images/indicator.gif"
width="32" height="32" alt="ReTweet" align="middle" border="0">
ReTweet</a>
<script type="text/javascript"
src="http://platform.twitter.com/widgets.js"></script>
<script type="text/javascript"><!--
cl.addContent({
content: '<a href="http://twitter.com/share"' +
' class="twitter-share-button" data-count="horizontal"' +
' data-via="jsclasses">' +
'<img ' +
' src="http://ajaxload.info/images/indicator.gif"' +
' width="32" height="32" alt="ReTweet" align="middle"' +
' border="0">' +
'ReTweet</a>' +
'<' + 'script type="text/javascript"' +
' src="http://platform.twitter.com/widgets.js"></' +
'script>',
inline: true
});
// --></script>
Here the content string was broken into multiple lines to make this article look better, but usually you do not need to do that.
The only thing you need to do is to break the HTML tags <script> and </script> into '<' + 'script>' and '</' + 'script>', so browsers do not confuse those tags in the string with the beginning or the end of JavaScript code sections.
You should repeat this procedure of replacing the original tags of slow content by delayed content JavaScript code tags until all slow content tags are replaced.
Loading slow content at the end of the page
The last step is to define the position in the page where the slow content should start loading. Usually it is the last thing in the page but it can be places before that position if you want slow content to start loading sooner.
<script type="text/javascript"><!--
cl.loadContent();
// --></script>
This last call to the content loader object will insert in the page the previously defined slow content inside hidden <div> tags. The object will wait whatever time it takes the browser to load the remote content.
Once the slow content loads, the object moves the DOM nodes of the just loaded slow content to the original positions. The the slow content starts appearing where it should, but without delaying the load the rest of the page.
Browser compatibility issues
The slow content loading procedure works fine in all browsers but for some reason under Internet Explorer and Opera the content may end up being loaded outside the hidden <div> tags added to the page by the loadContent function.
This is somewhat unexpected. So far no workaround was found to solve this problem. Therefore, in the current version of the object, when the browser is Internet Explorer or Opera, the object does not delay the loading of the slow content. Instead, it just inserts the content exactly where it was originally intended to appear. For these browsers the content will still slow down the load of the page as before.
If you have any ideas of how to avoid this problem of Internet Explorer and Opera, please post a comment to this article so you can help improving the content loader object browser support.
You need to be a registered user or login to post a comment
Login Immediately with your account on:
Comments:
1. Fast loading Ad JS - Eion Robb (2010-11-23 08:02)
Fast loading Ad JS... - 1 reply
Read the whole comment and replies