如何在呼叫时在函数中添加js?

时间:2012-02-23 02:19:07

标签: jquery

(function() {
    var jQuery;
    if (window.jQuery === undefined || window.jQuery.fn.jquery !== "1.7.1") {
        var script_tag = document.createElement("script");
        script_tag.setAttribute("type","text/javascript");
        script_tag.setAttribute("src",
            "https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js");
        var script_tag2 = document.createElement("script");
        script_tag2.setAttribute("type","text/javascript");
        script_tag2.setAttribute("src",
            "test.js");
        if (script_tag.readyState) {
          script_tag.onreadystatechange = function () { // For old versions of IE
              if (this.readyState == "complete" || this.readyState == "loaded") {
                  scriptLoadHandler();
              }
          };
        } else {
          script_tag.onload = scriptLoadHandler;
          script_tag2.onload = scriptLoadHandler;
        }
        (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
    } else {
        jQuery = window.jQuery;
        main();
    }

    function scriptLoadHandler() {
        jQuery = window.jQuery.noConflict(true);
        main();
    }

    function main() { 
        jQuery(document).ready(function($) {
            // code here ...
        });
    }
})();

当我运行代码导致firebug错误时,无法加载库jquery.min.js和test.js

1 个答案:

答案 0 :(得分:0)

我正在使用调用确保指定我的js,css,html需要特定的javascript函数并按需加载它。 它位于http://ensure.codeplex.com/

的codeplex上