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");
a.defer="true";a.src=src;
document.getElementsByTagName("head")[0].appendChild(a);
}
var dynloaded=false;
$(function() {
$(window).bind("scroll", function(event) {
if($(window).scrollTop() > 50){
if(dynloaded==false){
// ...
// javascript loading here, ex. jsload('script.js')
dynloaded=true;
}
}
});
});
</script>


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.

18 comments:

  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.

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

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

    ReplyDelete
  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.


    http://www.medicinaligenerici.com/



    ABOUT.......................


    Dynamic Javascript Loading on Scroll

    CONGRATULATION FOR THIS POST I HOPE YOU CREATE MORE POST SAME AS A FUTURE.


    THANKS FOR IT.

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

    ReplyDelete
  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 ()

    ReplyDelete
  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)

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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.

    ReplyDelete
  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.

    ReplyDelete