How do you ensure jQuery is available when loaded

2019-05-21 02:26发布

Google allows you to load jQuery asynchronously like so:

<script src="http://www.google.com/jsapi?key=..." type="text/javascript"></script> 
<script> 
  //<![CDATA[
    google.load('jquery', '1.6');
  //]]>
</script>
<script src="scripta.js" type="text/javascript"></script>
<script src="scriptb.js" type="text/javascript"></script>

In later scripts, how do I ensure jQuery has been loaded? I could use setInterval to check whether jQuery is set, but suppose scriptb.js depends on scripta.js, and both are wrapped in a setInterval to wait for jQuery, and so the interval will "randomly" execute either one first.

How can I approach this problem?

Thanks!

1条回答
Root(大扎)
2楼-- · 2019-05-21 02:59

Assign the optional setting callback (documentation):

google.load('jquery', '1.6', {"callback":jqueryLoaded});

function jqueryLoaded(){
alert('loaded');
}
查看更多
登录 后发表回答