[英]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.