Javascript 想要在一个文件中调用函数,而函数的定义在另一个文件中
我想在second.js文件中调用first.js文件中定义的函数。这两个文件都在HTML文件中定义,如: 我想在second.js中调用first.js中定义的fn1()。从我的搜索结果来看,答案是如果first.js是首先定义的,这是可能的,但从我的测试中,我还没有找到任何方法来做到这一点。谢谢 编辑: 以下是示例代码: second.jsJavascript 想要在一个文件中调用函数,而函数的定义在另一个文件中,javascript,html,Javascript,Html,我想在second.js文件中调用first.js文件中定义的函数。这两个文件都在HTML文件中定义,如: 我想在second.js中调用first.js中定义的fn1()。从我的搜索结果来看,答案是如果first.js是首先定义的,这是可能的,但从我的测试中,我还没有找到任何方法来做到这一点。谢谢 编辑: 以下是示例代码: second.js this.getSecEnabled = function(){ ----> i dont know how this is used her
this.getSecEnabled = function(){ ----> i dont know how this is used here..
return SecEnabled;
}
if( getSecEnabled() == "SecEnabled" ){
alert(" Security Enabled....!!");
}else{
alert(" Secuity Disbaled.!!");
}
first.js
this.getSecEnabled = function(){ ----> i dont know how this is used here..
return SecEnabled;
}
if( getSecEnabled() == "SecEnabled" ){
alert(" Security Enabled....!!");
}else{
alert(" Secuity Disbaled.!!");
}
问题:无法打印此处启用/禁用的任何语句
请帮助我,我是javascript新手,请尝试在第一个之前加载second.js。js尝试在第一个之前加载second.js。通常,如果您不想等到加载文档,则必须按以下顺序加载脚本:
- second.js
- first.js
通常,如果您不想等待文档加载,则必须按以下顺序加载脚本:
- second.js
- first.js
是的,你能做到。大多数最新浏览器都支持现成的ECMAScript模块。您需要做的只是: 确保在浏览器中链接所有这些链接,如下所示:
<script src="./path/to/script1.js" type="module" />
脚本2.js
是的,你能做到。大多数最新浏览器都支持现成的ECMAScript模块。您需要做的只是: 确保在浏览器中链接所有这些链接,如下所示:
<script src="./path/to/script1.js" type="module" />
脚本2.js
我有一个名为include()的函数 此函数的作用是告诉浏览器加载文件,然后执行操作 因此,该操作只能在加载文件后执行 你的情况也是如此;我们将首先包括first.js,然后包括second.js 因此,second.js将仅在first.js运行之后运行 代码如下:
function include(file){
return new Promise(function(resolve, reject){
var script = document.createElement('script');
script.src = file;
script.type ='text/javascript';
script.defer = true;
document.getElementsByTagName('head').item(0).appendChild(script);
script.onload = function(){
resolve()
}
script.onerror = function(){
reject()
}
})
/*I HAVE MODIFIED THIS TO BE PROMISE-BASED
HOW TO USE THIS FUNCTION
include('js/somefile.js').then(function(){
console.log('Do an action that depends on somefile.js');
},function(){
console.log('Do not do an action that depends on somefile.js');
})
*/
}
include('first.js')
.then(()=>{
include('second.js');
})
我有一个名为include()的函数 此函数的作用是告诉浏览器加载文件,然后执行操作 因此,该操作只能在加载文件后执行 你的情况也是如此;我们将首先包括first.js,然后包括second.js 因此,second.js将仅在first.js运行之后运行 代码如下:
function include(file){
return new Promise(function(resolve, reject){
var script = document.createElement('script');
script.src = file;
script.type ='text/javascript';
script.defer = true;
document.getElementsByTagName('head').item(0).appendChild(script);
script.onload = function(){
resolve()
}
script.onerror = function(){
reject()
}
})
/*I HAVE MODIFIED THIS TO BE PROMISE-BASED
HOW TO USE THIS FUNCTION
include('js/somefile.js').then(function(){
console.log('Do an action that depends on somefile.js');
},function(){
console.log('Do not do an action that depends on somefile.js');
})
*/
}
include('first.js')
.then(()=>{
include('second.js');
})
请检查first.js中的条件是否合适?是这样的吗?因为我在这行中出错了。。。如果(this.getSecEnabled()==“SecEnabled”){脚本在此行之后不继续,请将其更改为:getSecEnabled=function(){返回“SecEnabled”;}因为您没有声明名为SecEnabledyou想要更改second.js中函数??的定义,我们相信,我们可以在条件语句中进行更改。请让我知道first.js条件中的更改,但是getSecEnabled中有错误,或者您没有粘贴整个文件。正如我在那里所说的没有定义SecEnabled变量,这是一个问题。请检查first.js中的条件是否正确?是否类似于此?因为我在这一行中遇到错误…如果(this.getSecEnabled()==“SecEnabled”){脚本在这一行之后不继续,请将其更改为:getSecEnabled=function(){返回'SecEnabled';}因为您没有声明名为SecEnabledyou想要更改second.js中函数??的定义,我们相信,我们可以在条件语句中进行更改。请让我知道first.js条件中的更改,但是getSecEnabled中有错误,或者您没有粘贴整个文件。正如我在那里所说的没有定义SecEnabled变量,这是一个问题。请检查first.js中的条件是否正确?是否类似于此?因为我在这行中遇到错误…如果(this.getSecEnabled()=“SecEnabled”){脚本不在此行之后继续执行您不需要编写
此
-只需编写getSecEnabled()==“SecEnabled”
,因为现在您可以访问该函数,因为您将其导入了文件的顶部。我尝试了如下操作,但没有执行此语句。如果(getSecEnabled()==“SecEnabled”){仍然无法继续执行能否请您检查first.js中的条件是否正确?是这样的吗?因为我在这一行中遇到了错误…如果(this.getSecEnabled()==“SecEnabled”){脚本在这一行之后不继续执行您不需要写这
-只要写getSecEnabled()==“SecEnabled”
从现在起,您可以访问该函数,因为您在文件顶部导入了该函数。我尝试了如下操作,但没有使用此语句。如果(getSecEnabled()=“SecEnabled”){仍然无法执行从函数中删除“this”。因此在second.js中,它应该是:var getSecEnabled=function(){return SecEnabled;}另外,我不知道SecEnabled从何处提交。它是在其他地方定义的吗?请从函数中删除“this”。因此在second.js中,它应该是:var getSecEnabled=function(){return SecEnabled;}此外,我不知道SecEnabled从何处提交。它是在其他地方定义的吗?