Defer loading of JavaScript - Uncaught ReferenceError: $ is not defined

后端 未结 5 1341
鱼传尺愫
鱼传尺愫 2020-12-19 18:41

I use google code to defer loading javascript (google pages)

But I have some inline javascripts such as:



        
5条回答
  •  既然无缘
    2020-12-19 19:00

    If you are going to implement async loading of Javascripts, then it would be a better idea to not have inline Javascript which depends on the asynchronously loaded file.

    I suspect that you are loading jQuery asynchronously, i.e. after the DOM content has loaded, so any inline scripts in your DOM dependent on jQuery will fail. In that case, you may want to use something like RequireJS to manage your JS dependencies, and load them asynchronously.

    However, if you want to go for a simpler solution, I would suggest putting your JS libraries at the end of the DOM, and binding any dependencies to an onload handler.

    Something like this

    
        
        
    
    
    

提交回复
热议问题