DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Benjamin has posted 2 posts at DZone. View Full User Profile

Dynamic Javascript Loading On PageScroll

01.04.2011
| 2409 views |
  • submit to reddit
        // see http://www.myoutsourcedbrain.com/2011/01/dynamic-javascript-loading-on-scroll.html

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