[英]How could I use the variables from a remotely stored JavaScript file within the function of another JavaScript file?
Currently, I'm trying to take an array from a JavaScript file stored on a web page (ex. "website.com/javascript.js") and run it through a function stored in a JavaScript file I have made for a website. 目前,我正在尝试从存储在网页上的JavaScript文件(例如“ website.com/javascript.js”)中提取数组,并通过存储在为网站制作的JavaScript文件中的函数运行它。 This is for a game.
这是一个游戏。
Previously, I was attempting to import both .js files into the HTML file and use the array from the website as an input for my own JavaScript file, but I couldn't seem to do this. 以前,我曾尝试将两个.js文件导入HTML文件,并将网站中的数组用作我自己的JavaScript文件的输入,但我似乎无法做到这一点。
I simply couldn't find the answer online (or didn't know how to phrase the question right). 我只是无法在线找到答案(或者不知道如何正确表达问题)。 If I need to import anything for the purpose of doing this, could you tell me what to import?
如果需要为此目的导入任何东西,您能告诉我要导入什么吗? Thank you!
谢谢!
Here's the code I'm working on. 这是我正在处理的代码。 Sorry for the very novice code, I haven't worked much in JavaScript.
对非常新手的代码感到抱歉,我在JavaScript中工作不多。
function makeBracket(players){
var resultString;
for (var i = 0; i < players.length; i++){
var assassinName = players[i][1];
var targetCode = players[i][4];
for(var j=0;j<players.length;j++){
var code = players[j][2];
if(code == targetCode){
var targetName = players[j][1];
document.write(assassinName + " -> " + targetName + "<br>");
}
}
}
I'll be using the function.js
for the file that contains your function, and array.js/array.json
for the file that contains the data you need. 我将对包含您的函数的文件使用
function.js
,对包含所需数据的文件使用array.js/array.json
。
You could include both files, and expose the array on the first file as a global variable. 您可以同时包含两个文件,并将第一个文件上的数组公开为全局变量。 To include them, use two
script
tags on your HTML, in order: 要包括它们,请在HTML上依次使用两个
script
标签:
<script type="text/javascript" src="/array.js"></script>
<script type="text/javascript" src="/function.js"></script>
array.js
: array.js
:
window.myArray = [/* Data here */];
function.js
: function.js
:
var array = window.myArray;
doStuff(array);
An alternative is to store the array in JSON format, and load it asynchronously using AJAX. 另一种选择是将数组存储为JSON格式,然后使用AJAX异步加载。
Consider the following function.js
: 考虑以下
function.js
:
$.getJSON("/array.json", function(array) {
doStuff(array);
});
When loaded (I'm assuming jQuery here, since it is in the tags of the question), it will make a request to /array.json
, parse it as JSON, and pass the results to the callback function. 加载后(我假设这里是jQuery,因为它位于问题的标记中),它将向
/array.json
发出请求,将其解析为JSON,并将结果传递给回调函数。 From there, you can do anything with the data. 从那里,您可以对数据进行任何处理。
An advantage of this method is that you don't need to worry about ordering the files anymore, and you don't use any global variables. 这种方法的优点是您无需担心文件的顺序,也无需使用任何全局变量。 However, it is slightly more complex to understand, and there'll be a small delay between the page loading and the data arriving.
但是,它的理解要稍微复杂一点,并且页面加载和数据到达之间会有一个小的延迟。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.