您现在的位置是:首页 > 电脑技术查询 > web开发

请问两个js文件的函数互相访问的有关问题!

编辑:chaxungu时间:2022-10-02 23:15:31分类:web开发

请教两个js文件的函数互相访问的问题!!!
如 有 a.js 和 b.js

现在我要在 a.js文件中引用 b.js中的函数并传参过去。。。。

请问怎么实现啊?

下面这个试过 好像不行。。。
文件 a.js :
....
function toThat(){
...
load_operation(tdID,newName,type)
}
function load_operation(tdID,newName,type){

var head=document.getElementsByTagName('head');
var testScript=document.createElement('script');
testScript.src="operation.js ";
testScript.type='text/javascript';
head[0].appendChild(testScript);

renameFile(tdID,newName,type);//这就是operation.js中的函数

}

------解决方案--------------------
head[0].appendChild(testScript);
这一步之后,脚本加载还有个过程,而“renameFile()”却被立即调用了,此时它可能还没有加载到。

可以采取<script>标签的某些事件处理函数,来判断脚本是否加载完成,如onload或onreadystatechange,不过各浏览器的事件模型不一致,处理起来很麻烦。

也可以采取定时检测的方式:
JScript codefunction tryRenameFile() { if (typeof renameFile == 'function') { renameFile(tdID,newName,type); } else { setTimeout(tryRenameFile(), 100); // 只要还未检测到函数已经成功加载,则过100ms时间重新检测 }}
------解决方案--------------------
你还没有加载成功的呢,楼上的方法也不好
document.write('<script src="operation.js " type="text/javascript"></script>');
用我这一种就可以了
------解决方案--------------------
直接写到页面上面
HTML会自己编译的
------解决方案--------------------
document.write('<script src="operation.js " type="text/javascript"></script>');