从另一个JavaScript文件调用一个函数 [英] Calling a function from one JavaScript file which requires another
问题描述
我试图从另一个JavaScript文件调用一个JavaScript文件中写入的函数。我有以下代码,但它不起作用:
我的HTML文件
< script type =text / javascriptsrc =js1.js>< / script>
< script type =text / javascriptsrc =js2.js>< / script>
< script language =javascript>
js1();
< / script>
js1.js
函数js1()
{
alert(Hello from js1);
js2();
}
js2.js
函数js2()
{
alert(Hello from js2);
}
我能做什么?
尝试更改订单
< script type =text / javascriptsrc =js2.js>< / script>
< script type =text / javascriptsrc =js1.js>< / script>
< script language =javascript>
js1();
< / script>
因为你在里面调用 js2();
js1.js
,所以脚本 js2.js
应该在执行之前。
在你的情况下,我认为它应该仍然能够在不改变这种命令的情况下工作,因为你在函数内调用 js2();
。执行此脚本时:
函数js1()
{
alert(Hello from js1 );
js2();
}
即使 js2.js
目前尚未执行,但您不此时实际调用 js2();
。
试试看看它是否有效。
I am trying to call a function written in one JavaScript file from another JavaScript file. I have the following code, but it doesn't work:
My HTML file
<script type="text/javascript" src="js1.js"></script>
<script type="text/javascript" src="js2.js"></script>
<script language="javascript">
js1();
</script>
js1.js
function js1()
{
alert("Hello from js1");
js2();
}
js2.js
function js2()
{
alert("Hello from js2");
}
What can I do?
Try changing the order
<script type="text/javascript" src="js2.js"></script>
<script type="text/javascript" src="js1.js"></script>
<script language="javascript">
js1();
</script>
Because you call js2();
inside js1.js
, so the script js2.js
should be executed before.
In your case, i think it should still work without changing orders like this because you call js2();
inside a function. When this script is executed:
function js1()
{
alert("Hello from js1");
js2();
}
Even the js2.js
is not executed yet, but you do not actually call js2();
at this time.
Just try it to see if it works.
这篇关于从另一个JavaScript文件调用一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!