Jan 4, 2011

Dynamic Javascript Loading on Scroll

If you have a lot of widgets on your webpage page, you can try out many tools in order to speed up page loading, however there is a limit to optimization. It is often tried to put heavy javascripts at the end of page loading, putting them before <head> or using the window.onload function. In this post, I show an alternative that has the advantage of shorter initial page loading and a smoother user experience than these methods.

I had the idea when I was researching social toolbars. Often the social toolbar would appear only when the page was scrolled. Therefore page scrolling should be detected and if the page was scrolled down the toolbar would fade in.

I thought that the same code could be applied for dynamic javascript loading. Many libraries are not needed initially. Some widgets are only visible once the page is scrolled. I implemented some code myself for scroll event detection, however in the end, I use the ajax library for this purpose.

Here comes the general pattern:

<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js' type='text/javascript'/>
<script id='aptureScript' type='text/javascript'>
function jsload(src){
var a=document.createElement("script");
var dynloaded=false;
$(function() {
$(window).bind("scroll", function(event) {
if($(window).scrollTop() > 50){
// ...
// javascript loading here, ex. jsload('script.js')

The number, marked in red is the scrolled distance from the top of the page, at which the loading of additional javascript code will occur.

Please note that if you want to use this in your blogger template, make sure to escape html tags within the javascript code.

Enjoy. Please leave a comment below for questions and suggestions.


  1. No. This is not an install thing this time. I tagged the post with "programming" to make that clearer. It requires at least very basic skills in javascript to make this work.

    This example code can be useful if you have any script that takes a long to load (you can see that sometimes in the browser status bar or when you use an analysis tool). For example I found that the google followers widget took around 10 seconds to load. I reduced the time a lot by manually changing the number of rows from 4 to 2, so only half the images get loaded, however it is still slow (and sometimes even unresponsive). I found the code for the google follower widget, took out the parts within the <script> tags and put them in the middle part of the code (where it says loading here).

    Using a trick like this can reduce initial loading time a lot, but as I said, you should know a at least a little bit about javascript.

  2. Very useful post. This type of post should be spread so that others will know.

  3. Great article, could you please visit my blog on www.scasha.com

  4. Cialis

    Dopo le droghe
    come Viagra e Cialis ha rivoluzionato il trattamento di disfunzione sessuale
    maschio verso la fine degli anni 90, un turbine di neve dei test clinici sono
    stati condotti in donne nelle speranze che le droghe potrebbero fare lo
    stessi per fare rivivere l'azionamento di sesso diminuente della donna.



    Dynamic Javascript Loading on Scroll



  5. I totally agree with your post thanks for sharing,

  6. I'm not sure where you are getting your info, but good
    topic. I needs to spend some time learning more or understanding more.
    Thanks for wonderful information I was looking for this
    info for my mission.

    Look at my homepage ... usb rfid reader writer ()

  7. This car has a strong build and well known for its great
    safety credentials. The Bugatti Veyron is a very impressive car, powered by a W-16-cylinder 8.
    0 liter turbocharged diesel engine that is highly fuel efficient
    but is also available in the much less expensive 2010 VW Golf.

    my page ... 2015 Audi A3 Video (www.youtube.com)

  8. Hi there, You have done a great job.I will certainly digg it and personally recommend to
    my friends. I am confident they will be benefited from this
    web site.

    Also visit my page :: Tooth Whitening Pen

  9. I am curious to find out what blog platform you happen to be working with?
    I'm having some minor security problems with my latest blog and I would like to find something more risk-free.
    Do you have any suggestions?

    Here is my weblog live sex

  10. Simpson told her she would like the ending. Over the years Lisa had an on-and-off relationship with Simpson, but it would cheap Michael Kors wallets be closer in some ways than the one she had with her father.

  11. she hadn��t been able to finish it. Simpson told her she would like the ending. Over the years Lisa had an on-and-off relationship with Simpson, but it would be closer in some ways than michael kors outlet online the one she had with her father.

  12. Dali himself, ugg salg which makes cheap was very acid. sale, which is 12 flights billig uggs this month protection money, a total of ugg salg three hundred, the other classes yet to pay up. which ugg salg makes ugg salehim more charming side faces, the girls in the front row even want to kiss his face. cheap ugg salg smile on his face, but she gave it cheap to buy clothes, like my wife to her husband bought a new dress and her husband received the praise of others. However, there is no chea ugg salg Turned himself handsome, ugg australia norg wearing neat original have such an effect, original clothes make the man is like that. cheap newborn suddenly had the feeling, actually leaked a hint of a smile

  13. uggs uitverkoop Vijf jaar ugg classic short sale geleden, waren er te veel dingen gebeuren, of er gebeurt ieder jaar ook, heeft hij zich niet herinneren veel wikken en vijf jaar geleden iets te maken met de goedkope. uggs sale nederland Goedkope vijf jaar, uggs uitverkoop maar ook de dertien-jarige, een jongen acteur trofeeën ze hebben geen haat. Wie denkt niet uggs uitverkoop goedkoop is, maar uiteraard kinder uggs sale met eigen ogen heeft deze man haat.

  14. Ralph Lauren Soldes apparu, Ralph Lauren Soldes pas cher Jia Zhu était capable d'attaquer le champion le plus faible. Jappements. pas Ralph Lauren Soldes cher se précipita la bague à nouveau, Polo Ralph Lauren Pas Cher cette fois, Ralph Lauren Soldes pas cher a été tué. Ralph Lauren Soldes montée pas cher dans l'anneau. effectivement résisté sur la bague pas cher. il peut tenir. Wow, voir, la magie de la scène est

  15. Great post. This article is really very interesting and enjoyable. I think its must be helpful and informative for us. Thanks for sharing your nice post. i am Google plus provider . I am selling those service on, please take a look Buy Google plus Followers

  16. يعتبر شركة الضحى
    من اهم الشركات التى تقوم بخدمات التنظيف من الالف الى الياء

    فلدينا امكانية تنظيف الفلل – تنظيف المنازل –البيوت – المكاتب – الارضيات – الموكيت وغيرها من الاماكن كما ان اهتمام شركة الضحى بالتنظيف من الداخل والخارج

    فلا تتردد فى التواصل مع شركة تنظيف بالدمام
    الان ولدينا امكانية الوول الى اى مكان فى الدمام او خارج الدمام

    شركة نقل عفش بالرياض
    شركة تنظيف خزانات بالرياض
    شركة رش مبيدات ودفان بالرياض
    شركة نقل عفش بالدمام
    شركة تنظيف خزانات بالدمام
    شركة رش مبيدات ودفان بالدمام
    شركة تنظيف بالدمام
    شركة تنظيف منازل بالدمام
    شركة تسليك مجارى بالدمام
    شركة مكافحه حشرات بالدمام
    شركة كشف تسربات المياه بالدمام

    شركة الضحى

  17. اهم شركات نقل الاثاث بالرياض شركة اوائل الخليج

    وتفتخرشركة اوائل الخليج
    انها تقدم هذه الخدمات لاهل المملكه خدمات شركة نقل اثاث بالرياض

    هل تريد ان يكون النقل بسعر رخيص حتي تستطيع الدفع بسهوله ؟

    طلبك عندنا عزيزنا . . .ان شركة اوائل الخليج
    تقوم بنقل الأثاث من باب بيتك القديم الي الجديد في اماناً تام و لا تقلق علي عفشك او اثاثك من الكسر لأننا نهتم به تماماً . . . و يمكنك ان تشاهد بنفسك هذا عزيزي العميل

    أنت بنفسك ستكون موجوداً في عملية النقل . . . العمل سيتم تحت اشراف 4 مديرين للنقل و فريق عمل احترافي جداً و معتاد علي هذا العمل منذ الأزل و يستلموه اباً عن جداً لأن عملاً وراثي

    ننقل العفش او الأثاث الي اي مكان في المملكه و في امان تام علي عفشك او اثاثك المنزلي

    لا تقلق ابداً .

    شركة تنظيف بالرياض
    شركة تنظيف منازل بالرياض
    شركة تنظيف فلل بالرياض
    شركة تنظيف خزانات بالرياض
    شركة مكافحه حشرات بالرياض
    شركة تنظيف بيوت بالرياض
    شركة تنظيف مجالس بالرياض