简体   繁体   English

从另一个js源文件调用函数时发生未定义的错误

[英]undefined error when calling function from another js sourcefile

I have a question that concerns calling a function within another js sourcefile. 我有一个关于在另一个js源文件中调用函数的问题。

I declared the sourcefile before the file with the function that calls that particular function. 我在调用该特定函数的函数之前声明了源文件。 I thought this would work, but it gives an undefined error. 我以为这会工作,但是会出现未定义的错误。

I also have a function in a file that calls a function in a later declared sourcefile. 我在文件中也有一个函数,该文件在以后声明的源文件中调用一个函数。 That would concern the same files but the other way around. 那将涉及相同的文件,但反之亦然。

The functions are declared between document ready statements(functions). 这些函数在文档就绪语句(函数)之间声明。

Is there a way to avoid the undefined error?? 有没有办法避免未定义的错误?

this is how I set it up: 这是我设置的方式:

<script type="text/javascript" src="includes/livetabs.js"></script>
<script type="text/javascript" src="includes/chat.js"></script>


//this is in the chat.js file
$(document).ready(function(){

function chatWith(chatuser) {
    check=iscookieUsername();
    if (!check){
            chatusersuspended=chatuser;
            showchatinlogform();
    }
    createChatBox(chatuser);
    $("#chatbox_"+chatuser+" .chatboxtextarea").focus();
}


});

//this is in the livetabs.js file
$(document).ready(function(){

function iscookieUsername(){
        if (!tbusername){

            return false;
        }
        if (issessionusername){//flag if session has already been set

            return true;
        }
        $.ajax({
            url: "includes/livetabs.php", 
            data: ({username: tbusername, action: "setsessionusername"}),
            cache: false,
            type: "POST",
            dataType: "json",
            success: function(data) {
            //store username in php session
            //some personal user preferences returned 
            usernamecookie=data.cookie;
            trackuser=data.track;
            issessionusername=1;
        }});

        return true;
    }

});

Thanks, Richard 谢谢,理查德

If you have defined your function inside the $(document).ready method call, they won't be visible from outside. 如果您在$(document).ready方法调用中定义了函数,则它们从外部不可见。 Try to define them outside of the ready method call. 尝试在ready方法调用之外定义它们。

$(document).ready(
    function()
    {
        var test = function() { alert('test'); };

        test(); //It will work here.

        anotherTest(); //This will also work.

    }

);

function anotherTest() { alert('anotherTest'); };

test(); //You can't call test function here because test is defined inside the anonymous function passed to the 'ready' method.

anotherTest(); // Alerts 'anotherTest'. 

If this is not how your code is laid out, please post your source code so that the problem can be identified. 如果这不是您的代码的布局方式,请发布您的源代码,以便找出问题所在。

It's likely that your first source file, the one that contains the function you want to call, has a syntax error that prevents the function from being available. 您的第一个源文件(包含要调用的函数的文件)可能存在语法错误,导致该函数不可用。

Also, it doesn't matter which order your files are loaded, the function will be available anyways. 同样,文件的加载顺序无关紧要,该功能仍然可用。 Example: 例:

function foo() { alert('foo!'); }
foo();
bar();
function bar() { alert('bar!'); }

Edit: looks like SolutionYogi predicted the right answer. 编辑:看起来像SolutionYogi预测正确的答案。 But for reference, I'll leave mine up. 但作为参考,我会留下我的意见。

Use google chrome and press F12 to see Developer Tools before you browse that particular page. 浏览该特定页面之前,请使用google chrome并按F12键查看开发人员工具。 In Network you can see which files are loaded search for your script containing your function. 在“网络”中,您可以查看加载了哪些文件,然后搜索包含函数的脚本。 . (You can filter on Scripts also) it should be on top and your calling script should be loaded later. (您也可以在脚本上进行过滤)它应该在最上面,并且您的调用脚本应该在以后加载。 If it is not loaded then include that file in your page earlier. 如果尚未加载,请将该文件包含在页面中。 Hope that will solve the problem. 希望能解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM