jQuery没有';当它';这是后来加的

jQuery没有';当它';这是后来加的,jquery,Jquery,下面是我的JS的一个片段 如果PHP要求,它将加载JS函数。但对于我的问题来说,这并不重要 几乎在我的示例的底部,您会发现:loadjscssfile(“js/functions/”+data.action+“.js”,“js”) 它的作用是:假设data.action='helloworld'它将加载文件:js/functions/helloworld.js,并检查函数helloworld() 问题在最后一部分,helloworld.js被加载。但是当我执行:$.isFunction('hel

下面是我的JS的一个片段

如果PHP要求,它将加载JS函数。但对于我的问题来说,这并不重要

几乎在我的示例的底部,您会发现:
loadjscssfile(“js/functions/”+data.action+“.js”,“js”)
它的作用是:假设
data.action='helloworld'
它将加载文件:js/functions/helloworld.js,并检查函数
helloworld()
问题在最后一部分,helloworld.js被加载。但是当我执行:
$.isFunction('helloworld')
时,它不起作用

更新: jQuery函数AJAX实现了这一点,下面是解决方案

//
// LOAD JS OR CSS
//
var fileref;
function loadjscssfile(filename, filetype){
     if (filetype=="js"){ //if filename is a external JavaScript file
      fileref=document.createElement('script');
      fileref.setAttribute("type","text/javascript");
      fileref.setAttribute("src", filename);
     }
     else if (filetype=="css"){ //if filename is an external CSS file
      fileref=document.createElement("link");
      fileref.setAttribute("rel", "stylesheet");
      fileref.setAttribute("type", "text/css");
      fileref.setAttribute("href", filename);
     }
     if (typeof fileref!="undefined"){
        document.getElementsByTagName("head")[0].appendChild(fileref);
     }
}
//
// SET INIT AJAX FUNCTION
//
function init(i){
    $.ajax({
        dataType: "json",
        url: "ajax.php?i="+i
    }).done(function( data ) {
        if(data.menu!='N.U.'){
            $('.menu').html(data.menu);
        }
        if(data.container!='N.U.'){
            $('.container').html(data.container);
        }
        if(data.action!='N.U.'){
            if(data.action_val!='N.U.'){
                var funcCall = data.action + "('" + data.action_var + "');";
            } else {
                var funcCall = data.action + "();";
            }
            if($.isFunction(funcCall)){
                eval(funcCall); 
            } else {
                //
                // function doesnt excist try to load function from dir
                //
                loadjscssfile("js/functions/"+data.action+".js", "js");
                //
                // try again
                //
                if($.isFunction(funcCall)){
                    eval(funcCall); 
                } else {
                    //alert('FATAL ERROR: JS function ('+data.action+') doesnt excist');
                }
            }
        }
    }).fail(function(){
        alert( "error" );   
    });
}
解决方案

//
// SET INIT AJAX FUNCTION
//
function init(i){
    $.ajax({
        dataType: "json",
        url: "ajax.php?i="+i
    }).done(function( data ) {
        if(data.menu!='N.U.'){
            $('.menu').html(data.menu);
        }
        if(data.container!='N.U.'){
            $('.container').html(data.container);
        }
        if(data.action!='N.U.'){
            if(data.action_val!='N.U.'){
                var funcCall = data.action + "('" + data.action_var + "');";
            } else {
                var funcCall = data.action + "();";
            }

            if (typeof window[data.action] === "function") {
                eval(funcCall);
            } else {
                //
                // function doesnt excist try to load function from dir
                //
                $.ajax({
                  url: "js/functions/"+data.action+".js",
                  dataType: "script"
                }).done(function(){
                    //
                    // try again
                    //
                    if (typeof window[data.action] === "function") {
                        eval(funcCall); 
                    } else {
                        alert('FATAL ERROR: JS function ('+data.action+') doesnt excist');
                    }
                });
            }
        }
    }).fail(function(){
        alert( "error" );   
    });
}
当您创建这样的
标记时,通常在Javascript代码返回到主事件循环之后异步加载脚本。因此,在加载
loadjscssfile
后,脚本定义的函数将不会立即可用

您应该使用jQuery的
$.getScript()
函数。它接受一个回调函数,该函数将在加载脚本后调用


此外,您的检查
$。isFunction(funcCall)
不正确
funcCall
是一个字符串,类似于
“helloWorld()”
,而字符串不是函数。如果您想知道函数是否已加载,则必须执行
$.isFunction(window[data.action])
data.action
是函数的名称,而
window[data.action]
获取具有该名称的全局变量的值。

您确定正在加载文件吗?如果在文件中添加
Console.log
消息,是否会将其写入控制台?加载脚本后,应使用新添加脚本的onload处理程序将其用作回调。那么你的方法就可以用了,请你再解释一下;我真的不明白(谢谢)