admin管理员组文章数量:1414628
I wrap by $(function) both files for running code when page is ready. But for some reasons calling function from first file in second file gives me error "ReferenceError: test is not defined".
First file:
$(function() {
function test() {
alert(1);
}
});
Second file:
$(function() {
test();
});
I wrap by $(function) both files for running code when page is ready. But for some reasons calling function from first file in second file gives me error "ReferenceError: test is not defined".
First file:
$(function() {
function test() {
alert(1);
}
});
Second file:
$(function() {
test();
});
Share
Improve this question
edited Nov 18, 2017 at 9:31
jhpratt
7,13016 gold badges42 silver badges53 bronze badges
asked Nov 18, 2017 at 5:37
dt_dt_
951 silver badge11 bronze badges
1 Answer
Reset to default 7This is because JavaScript scope, you can avoid this by using Window global object.
Adding your variables to Window object will make them global, so you can access them from anywhere.
First file:
$(function() {
window.test = function () {
alert(1);
}
});
Second file:
$(function() {
test();
});
本文标签: javascriptFunction from other file is not definedStack Overflow
版权声明:本文标题:javascript - Function from other file is not defined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745171944a2646028.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论