0

我必须动态加载 JQuery 脚本,因为脚本有时收费,有时不收费。之后,我必须执行一个 JQuery 函数。我已经尝试过,但它不起作用。你能帮助我吗?这是我的代码:

<script>
    var script   = document.createElement("script");`
    script.type  = "text/javascript";
    script.src   = "http://code.jquery.com/jquery-1.7.1.min.js";    
    script.text  = "alert('voila!');"               
    document.body.appendChild(script);
    $(document).ready(function() {                                         
    getNumRecommendations("http://localhost:8080/elAbogado/","3974");});
</script>
4

1 回答 1

0

尝试这个:

function loadScript(url, callback)
{
    // adding the script tag to the head as suggested before
   var head = document.getElementsByTagName('head')[0];
   var script = document.createElement('script');
   script.type = 'text/javascript';
   script.src = url;

   // then bind the event to the callback function 
   // there are several events for cross browser compatibility
   script.onreadystatechange = callback;
   script.onload = callback;

   // fire the loading
   head.appendChild(script);
}

然后只是:loadScript("http://code.jquery.com/jquery-1.7.1.min.js", myCode);

来源:如何在另一个 JavaScript 文件中包含一个 JavaScript 文件?

于 2013-05-22T21:23:45.627 回答