繁体   English   中英

从另一个文件调用javascript函数

[英]calling javascript function from another file

我正在尝试完成以下操作: 从一个JavaScript文件调用一个函数,而该函数需要另一个JavaScript文件

本质上,我有index.php,这是主页的主要文件。 在索引文件中,我正在从另一个js文件调用javascript函数。

<script src="js/data.js"></script>
<script>(go_call())</script>

data.js

function go_call(){
     alert('working');
}

我收到以下错误: Uncaught ReferenceError: go_call is not defined

UPDATE 1:

我的原始代码在IE中工作正常,但是我在Google Chrome中出现此错误。

A function cannot be called unless it was defined in the same file or one loaded before the attempt to call it.

A function cannot be called unless it is in the same or greater scope then the one trying to call it.
It should work like this:

1) within data.js
function go_call(){
alert('working');
}

2) Within file2.js
function clickedTheButton() {
go_call();
}

3) Html File:
<html>
<head>
</head>
<body>
<button onclick="clickedTheButton()">Click me</button>
<script type="text/javascript" src="data.js"></script>
<script type="text/javascript" src="file2.js"></script>
</body>
</html>

暂无
暂无

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

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