动态加载JQuery文件并执行

Load JQuery file dynamically and execute it

本文关键字:执行 文件 JQuery 加载 动态      更新时间:2023-09-26

我必须动态加载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>

试试这个:

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文件?