繁体   English   中英

从其他js文件调用一个js函数?

[英]calling one js function from other js file?

我有两个.js文件(A,B)。 我想将B.js的函数调用到A.js文件中。 我怎么能完成。

A.js:

function validation(){
  alert("validating...");
}

B.js:

function login(){
  // i want to call validation() of file A.js here. How can it possible
  validation();

}

将a.js文件包含在b.js中

function includeJs(ajsfilepath) {
    var js = document.createElement("script");

    js.type = "text/javascript";
    js.src = jsFilePath;

    document.body.appendChild(js);
}

includeJs("/path/a.js");

如果您不想使用脚本动态包含javascript文件,可以在html文件中添加以下行:

<script src="A.js" type="text/javascript"></script>
<script src="B.js" type="text/javascript"></script>

然后,它应该工作。

暂无
暂无

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

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