Javascript includes not loading for later in the page

前端 未结 7 2668
我寻月下人不归
我寻月下人不归 2021-02-20 08:42

We have a Rails application where we are including our application dependencies in the html head within application.js:

//= require jquery
//= requi         


        
7条回答
  •  花落未央
    2021-02-20 08:55

    This can happen if you don't wait for the script to load that defines analytics or if you do not define the order in which the javascript files are loaded. Make sure that the script that defines analytics is always loaded before you try to call its method track. Depending on your setup the scripts could load in random order, leading to this unpredictable behavior.

    You tried to make sure everything was loaded, but the listener $(document).ready(function () {}); just makes sure that the DOM is ready, not that analytics is available. And here you have the same problem. $ is just jQuery so $ is not defined means jQuery hasn't been loaded yet. So probably your script came before jQuery was loaded and tried to call what wasn't defined yet.

提交回复
热议问题