У меня есть 2 JavaScript файлы:Как вызвать внешнюю функцию JavaScript в HTML?
FILE2.js
и FILE1.js
внутри FILE1.js 3 функции:
- FUNCTION1
- Function2
- FUNCTION3
Question1: Как я могу л FUNCTION2
внутри FUNCTION1
question2: Внутри HTML
я включаю FILE1
и FILE2
как:
<script type="text/javascript" src="Script/FILE1.js"></script>
<script type="text/javascript" src="Script/FILE2.js"></script>
Как сделать HTML, чтобы узнать, какой файл использовать, когда мне это нужно? и даже какой FUNCTION в этом файле?
Спасибо, и я ценю. OK вот коды
//return an array of values that match on a certain key
FILE1, function1
function function1(obj, key) {
var community = [];
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == 'object') {
community = community.concat(getValuesCommunity(obj[i], key));
} else if (i == key) {
community.push(obj[i]);
}
}
return community;
}
// split the string array into corresponding constituent
еще FILE1, но function2
function function2() {
community = community.split("/");
return community
я должен позвонить function1 в function2 или наоборот, и как это делается ?
obj==ACTIVITY as shown in the JSON
var ACTIVITIES =
{"games":
[
{"Definition1":
[
{"SPORTS" : "SPORTS"},
{"PHYSICAL SPORTS" : "SPORTS/PHYSICAL SPORTS"},
{"MENTAL SPORTS" : "SPORTS/MENTAL SPORTS"},
{"COMPUTER GAMES" : "SPORTS/COMPUTER GAMES"},
{"more" : "SPORTS/PHYSICAL GAMES/more"},
{"and more" : "SPORTS/PHYSICAL GAMES/more/and more"}
]
},
{"Definition2":
[
{"SPORTS" : "SPORTS"},
{"name" : "article2"}
]
}
]
};
если называть JSON
<script>
function myFunction() {
document.getElementById("loadJSON").innerHTML = getValuesCommunity(ACTIVITIES, "and more");
}
</script>
<div>
<button onclick="myFunction()">Load and Read JSON file</button>
<p id="loadJSON"></p>
</div>
Если дать мне список
SPORTS,PHYSICAL SPORTS, more, and more
Было бы полезно, если бы вы могли опубликовать код, который используете. – danmullen
@ danmullen Я включил код –