Search code examples
javascriptjquerywindowmodernizr

$(window).load() after Modernizr call... Not Working


I've this HTML

<head>
<script src="modernizr.js"></script>
<script>
    Modernizr.load([{
        // jQuery
        test: document.getElementsByClassName, // IE>8
        nope: 'js/jquery-1.11.3.min.js',
        yep: 'js/jquery-2.1.4.min.js'
        },{
        test: Modernizr.somefunction,
        load: 'somecode.js'
    }
</script>
</head>

And somecode.js

$(window).load(function(){
   //somecode
});

Doesn't work (Sometimes works, sometime doesn't only in Google Chrome).

I couldn't use $(document).ready() too, but could fix it with setTimeout with no seconds(Magic!)

$(document)ready(function(){
   setTimeout(function(){
      // some code
   });
});

Any idea to fix jQuery load method?


Solution

  • Sometimes, Modernizr works after some little objects load.

    To fix it:

    $(document)ready(function(){
       setTimeout(function(){
    
          function NewFunction(){
             // some code
             }
          // For Tags action
          if($('selector').find('otherselector').length > 0 ){
              NewFunction();
              }else{
                 $('selector').load(function(){
                    NewFunction(); 
                     });
                  }
            // For Attributes action      
          if((typeof($('selector').attr('attributename')) === 'undefined') || ($('selector').attr('attributename') === null)){
              NewFunction();
              }else{
                 $('selector').load(function(){
                    NewFunction(); 
                     }); 
                  }
            // Width | Height img
            if($('imgselector').height() !== '0px'){
                NewFunction();
              }else{
                 $('imgselector').load(function(){
                    NewFunction(); 
                     });
                  }
    
       });
    });