0

I searched my site on this https://developers.google.com/speed/pagespeed/insights/ it showed I have Render blocking javascript (jquery).. so I put async

<script src="js/jquery-3.1.1.min.js" async></script>

after that I got the following error..

Uncaught ReferenceError: $ is not defined

for the following jquery dependent code

$(function(){
    $('.SOMECLASS').on('click', function(){
       //some action
    })
})

So I changed to the following

document.addEventListener("DOMContentLoaded", function(event) { 
    $('.SOMECLASS').on('click', function(){
       //some action
    })
})

But still it is throwing the same error. also tried

window.onload = function(event) { 
    $('.SOMECLASS').on('click', function(){
       //some action
    })
}

but it if add window.onload click functions doesn't work..

How to make the code run after jquery is loaded?

Please help me..

scriptkiddie1
  • 497
  • 2
  • 6
  • 17

2 Answers2

0

This error is caused by "async" because jquery doesn't async support if you want async load then use jQI(http://www.yterium.net/jQl-an-asynchronous-jQuery-Loader) or requirejs javascript library.

muratoner
  • 2,316
  • 3
  • 20
  • 32
  • Thank you bro my problem is I want to reduce render blocking scripts.. If I add your script I am afraid that it will also become another render blocking script. – scriptkiddie1 Mar 08 '17 at 12:32
  • I'm understand you but only use jQI or requirejs then other all javascript library on it load. – muratoner Mar 08 '17 at 12:38
0

In case anybody in future come with the same problem.. I put all the jquery dependent code in the external script and called and make async and defer that solved it..

scriptkiddie1
  • 497
  • 2
  • 6
  • 17